Part Number Hot Search : 
D100N 1A194 D5V0S 99002E3 B5950 21PA1 DS2175N UESD56B
Product Description
Full Text Search
 

To Download ATMEGA163L-4AC Datasheet File

  If you can't view the Datasheet, Please click here to try to view without PDF Reader .  
 
 


  Datasheet File OCR Text:
  1 features ? high-performance, low-power avr ? 8-bit microcontroller ? 130 powerful instructions ? most single clock cycle execution ? 32 x 8 general purpose working registers ? fully static operation ? up to 8 mips throughput at 8 mhz ? on-chip 2-cycle multiplier  nonvolatile program and data memories  self-programming in-system programmable flash memory ? 16k bytes with optional boot block (256 - 2k bytes) endurance: 1,000 write/erase cycles ? boot section allows reprogramming of program code without external programmer ? optional boot code section with independent lock bits ? 512 bytes eeprom endurance: 100,000 write/erase cycles ? 1024 bytes internal sram ? programming lock for software security  peripheral features ? two 8-bit timer/counters with separate prescaler and compare mode ? one 16-bit timer/counter with separate prescaler, compare mode, and capture mode ? real time clock with separate oscillator and counter mode ? three pwm channels ? 8-channel, 10-bit adc ? byte-oriented two-wire serial interface ? programmable serial uart ? master/slave spi serial interface ? programmable watchdog timer with separate on-chip oscillator ? analog comparator  special microcontroller features ? power-on reset and programmable brown-out detection ? internal calibrated rc oscillator ? external and internal interrupt sources ? four sleep modes: idle, adc noise reduction, power-save, and power-down  power consumption at 4 mhz, 3.0v, 25 c ? active 5.0 ma ? idle mode 1.9 ma ? power-down mode < 1 a  i/o and packages ? 32 programmable i/o lines ? 40-pin pdip and 44-pin tqfp  operating voltages ? 2.7 - 5.5v for atmega163l ? 4.0 - 5.5v for atmega163  speed grades ? 0 - 4 mhz for atmega163l ? 0 - 8 mhz for atmega163 8-bit microcontroller with 16k bytes in-system programmable flash atmega163 atmega163l not recommend for new designs. use atmega16. rev. 1142e?avr?02/03
2 atmega163(l) 1142e?avr?02/03 pin configurations (scl) (sda) (sda) (scl)
3 atmega163(l) 1142e?avr?02/03 description the atmega163 is a low-power cmos 8-bit microcontroller based on the avr architec- ture. by executing powerful instructions in a single clock cycle, the atmega163 achieves throughputs approaching 1 mips per mhz allowing the system designer to optimize power consumption versus processing speed. block diagram figure 1. block diagram the avr core combines a rich instruction set with 32 general purpose working registers. all the 32 registers are directly connected to the arithmetic logic unit (alu), allowing two independent registers to be accessed in one single instruction executed in one clock program counter internal oscillator watchdog timer stack pointer program flash mcu control register sram general purpose registers instruction register timer/ counters instruction decoder data dir. reg. portb data dir. reg. porta data dir. reg. portd data dir. reg. portc data register portb data register porta analog mux adc data register portd data register portc programming logic timing and control oscillator oscillator interrupt unit eeprom spi uart status register z y x alu portb drivers porta drivers portd drivers portc drivers pb0 - pb7 pa0 - pa7 reset vcc avcc agnd aref gnd xtal2 xtal1 control lines + - analog comp ara tor pd0 - pd7 pc0 - pc7 8-bit data bus 2-wire serial interface internal reference internal calibrated oscillator
4 atmega163(l) 1142e?avr?02/03 cycle. the resulting architecture is more code efficient while achieving throughputs up to ten times faster than conventional cisc microcontrollers. the atmega163 provides the following features: 16k bytes of in-system self-program- mable flash, 512 bytes eeprom, 1024 bytes sram, 32 general purpose i/o lines, 32 general purpose working registers, three flexible timer/counters with compare modes, internal and external interrupts, a byte oriented two-wire serial interface, an 8-channel, 10-bit adc, a programmable watchdog timer with internal oscillator, a programmable serial uart, an spi serial port, and four software selectable power saving modes. the idle mode stops the cpu while allowing the sram, timer/counters, spi port, and inter- rupt system to continue func tioning. the power-down mode saves the register contents but freezes the oscillator, disabling all other chip functions until the next interrupt or hardware reset. in power-save mode, the asynchronous timer oscillator continues to run, allowing the user to maintain a timer base while the rest of the device is sleeping. the adc noise reduction mode stops the cpu and all i/o modules except asynchro- nous timer and adc, to minimize switching noise during adc conversions. the on-chip isp flash can be programmed through an spi serial interface or a conven- tional programmer. by installing a self-programming boot loader, the microcontroller can be updated within the application without any external components. the boot pro- gram can use any interface to download the application program in the application flash memory. by combining an 8-bit cpu with in-system self-programmable flash on a monolithic chip, the atmel atmega163 is a powerful microcontroller that provides a highly flexible and cost effective solution to many embedded control applications. the atmega163 avr is supported with a fu ll suite of program and system development tools including: c compilers, macro assemblers, program debugger/simulators, in-cir- cuit emulators, and evaluation kits. pin descriptions vcc digital supply voltage. gnd digital ground. port a (pa7..pa0) port a serves as the analog inputs to the a/d converter. port a also serves as an 8-bit bi-directional i/o port, if the a/d converter is not used. port pins can provide internal pull-up resistors (selected for each bit). the port a output buffers can sink 20ma and can drive led displays directly. when pins pa0 to pa7 are used as inputs and are externally pulled low, they will source current if the internal pull- up resistors are activated. the port a pins are tristated when a reset condition becomes active, even if the clock is not running. port b (pb7..pb0) port b is an 8-bit bi-directional i/o port with internal pull-up resistors (selected for each bit). the port b output buffers can sink 20 ma. as inputs, port b pins that are externally pulled low will source current if the pull-up resistors are activated. port b also serves the functions of various special features of the atmega83/163 as listed on page 117. the port b pins are tristated when a reset condition becomes active, even if the clock is not running. port c (pc7..pc0) port c is an 8-bit bi-directional i/o port with internal pull-up resistors (selected for each bit). the port c output buffers can sink 20 ma. as inputs, port c pins that are externally pulled low will source current if the pull-up resistors are activated. the port c pins are tristated when a reset condition becomes active, even if the clock is not running.
5 atmega163(l) 1142e?avr?02/03 port c also serves the functions of various special features of the atmega163 as listed on page 124. port d (pd7..pd0) port d is an 8-bit bidirectional i/o port with internal pull-up resistors (selected for each bit). the port d output buffers can sink 20 ma. as inputs, port d pins that are externally pulled low will source current if the pull-up resistors are activated. port d also serves the functions of various special features of the atmega163 as listed on page 128. the port d pins are tristated when a reset condition becomes active, even if the clock is not running. reset reset input. a low level on this pin for more than 500 ns will generate a reset, even if the clock is not running. shorter pulses are not guaranteed to generate a reset. xtal1 input to the inverting oscillator amplifier and input to the internal clock operating circuit. xtal2 output from the inverting oscillator amplifier. avcc this is the supply voltage pin for port a and the a/d converter. it should be externally connected to v cc , even if the adc is not used. if the adc is used, it should be con- nected to v cc through a low-pass filter. see page 105 for details on operation of the adc. aref aref is the analog reference input pin for the a/d converter. for adc operations, a voltage in the range 2.5v to avcc can be applied to this pin. agnd analog ground. if the board has a separate analog ground plane, this pin should be con- nected to this ground plane. otherwise, connect to gnd. clock options the device has the following clock source options, selectable by flash fuse bits as shown: note: 1. ?1? means unprogrammed, ?0? means programmed. the various choices for each clocking option give different start-up times as shown in table 5 on page 25. internal rc oscillator the internal rc oscillator option is an on-chip oscillator running at a fixed frequency of nominally 1 mhz. if selected, the device can operate with no external components. the device is shipped with this option selected. see ?eeprom read/write access? on page 62 for information on calibrating this oscillator. table 1. device clocking options select (1) device clocking option cksel3..0 external crystal/ceramic resonator 1111 - 1010 external low-frequency crystal 1001 - 1000 external rc oscillator 0111 - 0101 internal rc oscillator 0100 - 0010 external clock 0001 - 0000
6 atmega163(l) 1142e?avr?02/03 crystal oscillator xtal1 and xtal2 are input and output, respectively, of an inverting amplifier which can be configured for use as an on-chip oscillator, as shown in figure 2. either a quartz crystal or a ceramic resonator may be used. figure 2. oscillator connections external clock to drive the device from an external clock source, xtal1 should be driven as shown in figure 3. figure 3. external clock drive configuration external rc oscillator for timing insensitive applications, the external rc configuration shown in figure 4 can be used. for details on how to choose r and c, see table 64 on page 162. figure 4. external rc configuration timer oscillator for the timer oscillator pins, tosc1 and tosc2, the crystal is connected directly between the pins. no external capacitors are needed. the oscillator is optimized for use with a 32,768 hz watch crystal. applying an external clock source to the tosc1 pin is not recommended. xtal2 xtal1 gnd c r v cc nc
7 atmega163(l) 1142e?avr?02/03 architectural overview the fast-access register file concept contains 32 x 8-bit general purpose working reg- isters with a single clock cycle access time. this means that during one single clock cycle, one arithmetic logic unit (alu) operat ion is executed. tw o operands are output from the register file, the operation is executed, and the result is stored back in the register file ? in one clock cycle. six of the 32 registers can be used as three 16-bits indirect address register pointers for data space addressing ? enabling efficient address calculations. one of the three address pointers is also used as the address pointer for look-up tables in flash program memory. these added function registers are the 16-bits x-, y-, and z-register. the alu supports arithmetic and logic oper ations between registers or between a con- stant and a register. single register operations are also executed in the alu. figure 5 shows the atmega163 avr enhanced risc microcontroller architecture. in addition to the register operation, the conventional memory addressing modes can be used on the register file as well. this is enabled by the fact that the register file is assigned the 32 lowest data space addresses ($00 - $1f), allowing them to be accessed as though they were ordinary memory locations. the i/o memory space contains 64 addresses for cpu peripheral functions as control registers, timer/counters, a/d-converters, and other i/o functions. the i/o memory can be accessed directly, or as the data space locations following those of the register file, $20 - $5f.
8 atmega163(l) 1142e?avr?02/03 figure 5. the atmega163 avr risc architecture the avr uses a harvard architecture concept ? with separate memories and buses for program and data. the program memory is executed with a two stage pipeline. while one instruction is being executed, the next instruction is pre-fetched from the program memory. this concept enables instructions to be executed in every clock cycle. the program memory is in-system re-programmable flash memory. with the jump and call instructions, the whole 8k word address space is directly accessed. most avr instructions have a single 16-bit word format. every program memory address contains a 16- or 32-bit instruction. program flash memory space is divided in two sections, the boot program section (256 to 2,048 bytes, see page 134) and the application program section. both sections have dedicated lock bits for write and read/write protection. the spm instruction that writes into the application flash memory section is allowed only in the boot program section. during interrupts and subroutine calls, the return address program counter (pc) is stored on the stack. the stack is effectively allocated in the general data sram, and consequently the stack size is only limited by the total sram size and the usage of the sram. all user programs must initialize the sp in the reset routine (before subroutines or interrupts are executed). the 11-bit stack pointer sp is read/write accessible in the i/o space. 8k x 16 program memory instruction register instruction decoder program counter control lines 32 x 8 general purpose registrers alu status and control interrupt unit spi unit 8-bit timer/counter watchdog timer a/d converter analog comparator 32 i/o lines 512 x 8 eeprom data bus 8-bit two-wire serial interface 16-bit timer/counter with pwm 8-bit timer/counter with pwm 1024 x 8 data sram direct addressing indirect addressing serial uart
9 atmega163(l) 1142e?avr?02/03 the 1,024 bytes data sram can be easily accessed through the five different address- ing modes supported in the avr architecture. the memory spaces in the avr architecture are all linear and regular memory maps. a flexible interrupt module has its control registers in the i/o space with an additional global interrupt enable bit in the status register. all interrupts have a separate interrupt vector in the interrupt vector table at the beginning of the program memory. the inter- rupts have priority in accordance with their interrupt vector position. the lower the interrupt vector address, the higher the priority. figure 6. memory maps $0000 $1fff program memory application flash section boot flash section
10 atmega163(l) 1142e?avr?02/03 the general purpose register file figure 7 shows the structure of the 32 general purpose working registers in the cpu. figure 7. avr cpu general purpose working registers all the register operating instructions in the instruction set have direct and single cycle access to all registers. the only exception is the five constant arithmetic and logic instructions sbci, subi, cpi, andi, and ori between a constant and a register, and the ldi instruction for load immediate constant data. these instructions apply to the second half of the registers in the register file ? r16..r31. the general sbc, sub, cp, and, and or and all other operations betw een two registers or on a single register apply to the entire register file. as shown in figure 7, each register is also assigned a data memory address, mapping them directly into the first 32 locations of the user data space. although not being phys- ically implemented as sram locations, this memory organization provides great flexibility in access of the registers, as t he x-, y-, and z-registers can be set to index any register in the file. the x-register, y-register, and z-register the registers r26..r31 have some added functions to their general purpose usage. these registers are address pointers for indirect addressing of the data space. the three indirect address registers x, y, and z are defined as: figure 8. the x-, y-, and z-registers 7 0 addr. r0 $00 r1 $01 r2 $02 ? r13 $0d general r14 $0e purpose r15 $0f working r16 $10 registers r17 $11 ? r26 $1a x-register low byte r27 $1b x-register high byte r28 $1c y-register low byte r29 $1d y-register high byte r30 $1e z-register low byte r31 $1f z-register high byte 15 xh xl 0 x - register 7 0 7 0 r27 ($1b) r26 ($1a) 15 yh yl 0 y - register 7 0 7 0 r29 ($1d) r28 ($1c) 15 zh zl 0 z - register 7 0 7 0 r31 ($1f) r30 ($1e)
11 atmega163(l) 1142e?avr?02/03 in the different addressing modes these address registers have functions as fixed dis- placement, automatic increment and decrement (see the descriptions for the different instructions). the alu ? arithmetic logic unit the high-performance avr alu operates in direct connection with all the 32 general purpose working registers. within a single clock cycle, alu operations between regis- ters in the register file are executed. the alu operations are divided into three main categories ? arithmetic, logical, and bit-functions. atmega163 also provides a powerful multiplier supporting both signed/unsigned multiplication and fractional format. see the instruction set section for a detailed description. the in-system self- programmable flash program memory the atmega163 contains 16k bytes on-chip in-system self-programmable flash memory for program storage. since all instructions are 16- or 32-bit words, the flash is organized as 8k x 16. the flash program memory space is divided in two sections, boot program section and application program section. the flash memory has an endurance of at least 1,000 write/erase cycles. the atmega163 program counter (pc) is 13 bits wide, thus addressing the 8,192 program memory locations. the operation of boot program section and associated boot lock bits for software protection are described in detail on page 134. see also page 154 for a detailed description on flash data serial downloading. constant tables can be allocated within the entire program memory address space (see the lpm ? load program memory instruction description). see also page 12 for the different program memory addressing modes. the sram data memory figure 9 shows how the atmega163 sram memory is organized. figure 9. sram organization register file r0 r1 r2 r29 r30 r31 i/o registers $00 $01 $02 ... $3d $3e $3f ... $0000 $0001 $0002 $001d $001e $001f $0020 $0021 $0022 ... $005d $005e $005f ... data address space $0060 $0061 $045e $045f ... internal sram
12 atmega163(l) 1142e?avr?02/03 the lower 1,120 data memory locations address the register file, the i/o memory, and the internal data sram. the first 96 locations address the register file + i/o memory, and the next 1,024 locations address the internal data sram. the five different addressing modes for the data memory cover: direct, indirect with dis- placement, indirect, indirect with pre-decrement, and indirect with post-increment. in the register file, registers r26 to r31 feature the indirect addressing pointer registers. the direct addressing reaches the entire data space. the indirect with displacement mode features a 63 address locations reach from the base address given by the y- or z-register. when using register indirect addressing modes with automatic pre-decrement and post- increment, the address registers x, y, and z are decremented and incremented. the 32 general purpose working registers, 64 i/o registers, and the 1,024 bytes of internal data sram in the atmega163 are all accessible through all these addressing modes. the program and data addressing modes the atmega163 avr enhanced risc microcontroller supports powerful and efficient addressing modes for access to the program memory (flash) and data memory (sram, register file, and i/o memory). this section describes the different addressing modes supported by the avr architecture. in the figures, op means the operation code part of the instruction word. to simplify, not all figures show the exact location of the addressing bits. register direct, single register rd figure 10. direct single register addressing the operand is contained in register d (rd).
13 atmega163(l) 1142e?avr?02/03 register direct, two registers rd and rr figure 11. direct register addressing, two registers operands are contained in register r (rr) and d (rd). the result is stored in register d (rd). i/o direct figure 12. i/o direct addressing operand address is contained in 6 bits of the instruction word. n is the destination or source register address. data direct figure 13. direct data addressing a 16-bit data address is contained in the 16 lsbs of a two-word instruction. rd/rr specify the destination or source register. op rr/rd 16 31 15 0 16 lsbs $0000 $045f 20 19 data space
14 atmega163(l) 1142e?avr?02/03 data indirect with displacement figure 14. data indirect with displacement operand address is the result of the y- or z-register contents added to the address con- tained in 6 bits of the instruction word. data indirect figure 15. data indirect addressing operand address is the contents of the x-, y-, or the z-register. data indirect with pre- decrement figure 16. data indirect addressing with pre-decrement the x-, y-, or the z-register is decremented before the operation. operand address is the decremented contents of the x-, y-, or the z-register. data space $0000 $045f y or z - register op a n 0 0 5 6 10 15 15 data space $0000 $045f x, y or z - register 0 15 data space $0000 $045f x, y or z - register 0 15 -1
15 atmega163(l) 1142e?avr?02/03 data indirect with post- increment figure 17. data indirect addressing with post-increment the x-, y-, or the z-register is incremented after the operation. operand address is the content of the x-, y-, or the z-register prior to incrementing. constant addressing using the lpm and spm instructions figure 18. code memory constant addressing constant byte address is specified by the z-register contents. the 15 msbs select word address (0 - 8k). for lpm, the lsb selects low byte if cleared (lsb = 0) or high byte if set (lsb = 1). for spm, the lsb should be cleared. indirect program addressing, ijmp and icall figure 19. indirect program memory addressing program execution continues at address contained by the z-register (i.e., the pc is loaded with the contents of the z-register). data space $0000 $045f x, y or z - register 0 15 1 $1fff $1fff
16 atmega163(l) 1142e?avr?02/03 relative program addressing, rjmp and rcall figure 20. relative program memory addressing program execution continues at address pc + k + 1. the relative address k is from -2,048 to 2,047. the eeprom data memory the atmega163 contains 512 bytes of data eeprom memory. it is organized as a sep- arate data space, in which single bytes can be read and written. the eeprom has an endurance of at least 100,000 write/erase cycles. the access between the eeprom and the cpu is described on page 62 specifying the eeprom address registers, the eeprom data register, and the eeprom control register. for the spi data downloading, see page 154 for a detailed description. memory access times and instruction execution timing this section describes the general access timing concepts for instruction execution and internal memory access. the avr cpu is driven by the system clock ?, directly generated from the main oscil- lator for the chip. no internal clock division is used. figure 21 shows the parallel instruction fetches and instruction executions enabled by the harvard architecture and the fast-access register file concept. this is the basic pipelining concept to obtain up to 1 mips per mhz with the corresponding unique results for functions per cost, functions pe r clocks, and functions per power-unit. figure 21. the parallel instruction fetches and instruction executions figure 22 shows the internal timing concept for the register file. in a single clock cycle an alu operation using two register operands is executed, and the result is stored back to the destination register. $1fff 1 system clock ? 1st instruction fetch 1st instruction execute 2nd instruction fetch 2nd instruction execute 3rd instruction fetch 3rd instruction execute 4th instruction fetch t1 t2 t3 t4
17 atmega163(l) 1142e?avr?02/03 figure 22. single cycle alu operation the internal data sram access is performed in two system clock cycles as described in figure 23. figure 23. on-chip data sram access cycles i/o memory the i/o space definition of the atmega163 is shown in the following table: system clock ? total execution time register operands fetch alu operation execute result write back t1 t2 t3 t4 system clock ? wr rd data data address address t1 t2 t3 t4 prev. address read write table 2. atmega163 i/o space (1) i/o address (sram address) name function $3f ($5f) sreg status register $3e ($5e) sph stack pointer high $3d ($5d) spl stack pointer low $3b ($5b) gimsk general interrupt mask register $3a ($5a) gifr general interrupt flag register $39 ($59) timsk timer/counter interrupt mask register $38 ($58) tifr timer/counter interrupt flag register $37 ($57) spmcr spm control register $36 ($56) twcr two-wire serial interface control register $35 ($55) mcucr mcu general control register $34 ($54) mcusr mcu general status register $33 ($53) tccr0 timer/counter0 control register
18 atmega163(l) 1142e?avr?02/03 $32 ($52) tcnt0 timer/counter0 (8-bit) $31 ($51) osccal oscillator calibration register $30 ($50) sfior special function i/o register $2f ($4f) tccr1a timer/counter1 control register a $2e ($4e) tccr1b timer/counter1 control register b $2d ($4d) tcnt1h timer/counter1 high-byte $2c ($4c) tcnt1l timer/counter1 low-byte $2b ($4b) ocr1ah timer/counter1 output compare register a high-byte $2a ($4a) ocr1al timer/counter1 output compare register a low-byte $29 ($49) ocr1bh timer/counter1 output compare register b high-byte $28 ($48) ocr1bl timer/counter1 output compare register b low-byte $27 ($47) icr1h t/c 1 input capture register high-byte $26 ($46) icr1l t/c 1 input capture register low-byte $25 ($45) tccr2 timer/counter2 control register $24 ($44) tcnt2 timer/counter2 (8-bit) $23 ($43) ocr2 timer/counter2 output compare register $22 ($42) assr asynchronous mode status register $21 ($41) wdtcr watchdog timer control register $20 ($40) ubrrhi uart baud rate register high-byte $1f ($3f) eearh eeprom address register high-byte $1e ($3e) eearl eeprom addr ess register low-byte $1d ($3d) eedr eeprom data register $1c ($3c) eecr eeprom control register $1b ($3b) porta data register, port a $1a ($3a) ddra data direction register, port a $19 ($39) pina input pins, port a $18 ($38) portb data register, port b $17 ($37) ddrb data direction register, port b $16 ($36) pinb input pins, port b $15 ($35) portc data register, port c $14 ($34) ddrc data direction register, port c $13 ($33) pinc input pins, port c $12 ($32) portd data register, port d $11 ($31) ddrd data direction register, port d $10 ($30) pind input pins, port d table 2. atmega163 i/o space (continued) (1) i/o address (sram address) name function
19 atmega163(l) 1142e?avr?02/03 note: 1. reserved and unused locations are not shown in the table. all atmega163 i/os and peripherals are placed in the i/o space. the i/o locations are accessed by the in and out instructions, transferring data between the 32 general pur- pose working registers and the i/o space. i/o registers within the address range $00 - $1f are directly bit-accessible using the sbi and cbi instructions. in these registers, the value of single bits can be checked by using the sbis and sbic instructions. refer to the instruction set chapter for more details. when using the i/o specific commands in and out, the i/o addresses $00 - $3f must be used. when addressing i/o registers as sram, $20 must be added to these addresses. all i/o register addresses throughout this document are shown with the sram address in parentheses. for compatibility with future devices, reserved bits should be written to zero if accessed. reserved i/o memory addresses should never be written. some of the status flags are cleared by writing a logical one to them. note that the cbi and sbi instructions will operate on all bits in the i/o register, writing a one back into any flag read as set, thus clearing the flag. the cbi and sbi instructions work with reg- isters $00 to $1f only. the i/o and peripherals control registers are explained in the following sections. $0f ($2f) spdr spi i/o data register $0e ($2e) spsr spi status register $0d ($2d) spcr spi control register $0c ($2c) udr uart i/o data register $0b ($2b) ucsra uart control and status register a $0a ($2a) ucsrb uart control and status register b $09 ($29) ubrr uart baud rate register $08 ($28) acsr analog comparator control and status register $07 ($27) admux adc multiplexer select register $06 ($26) adcsr adc control and status register $05 ($25) adch adc data register high $04 ($24) adcl adc data register low $03 ($23) twdr two-wire serial interface data register $02 ($22) twar two-wire serial interface (slave) address register $01 ($21) twsr two-wire serial interface status register $00 ($20) twbr two-wire serial interface bit rate register table 2. atmega163 i/o space (continued) (1) i/o address (sram address) name function
20 atmega163(l) 1142e?avr?02/03 the status register ? sreg the avr status register ? sreg ? at i/o space location $3f ($5f) is defined as:  bit 7 ? i: global interrupt enable the global interrupt enable bit must be set (one) for the interrupts to be enabled. the individual interrupt enable control is then performed in the interrupt mask registers. if the global interrupt enable register is cleared (zero), none of the interrupts are enabled independent of the values of the interrupt mask registers. the i-bit is cleared by hard- ware after an interrupt has occurred, and is set by the reti instruction to enable subsequent interrupts.  bit 6 ? t: bit copy storage the bit copy instructions bld (bit load) and bst (bit store) use the t-bit as source and destination for the operated bit. a bit from a register in the register file can be cop- ied into t by the bst instruction, and a bit in t can be copied into a bit in a register in the register file by the bld instruction.  bit 5 ? h: half carry flag the half carry flag h indicates a half carry in some arithmetic operations. see the instruction set description for detailed information.  bit 4 ? s: sign bit, s = n v the s-bit is always an exclusive or between the negative flag n and the two?s comple- ment overflow flag v. see the instruction set description for detailed information.  bit 3 ? v: two?s complement overflow flag the two?s complement overflow flag v supports two?s complement arithmetics. see the instruction set description for detailed information.  bit 2 ? n: negative flag the negative flag n indicates a negative result in an arithmetic or logic operation. see the instruction set description for detailed information.  bit 1 ? z: zero flag the zero flag z indicates a zero result in an arithmetic or logic operation. see the instruction set description for detailed information.  bit 0 ? c: carry flag the carry flag c indicates a carry in an arithmetic or logic operation. see the instruction set description for detailed information. note that the status register is not automatically stored when entering an interrupt rou- tine and restored when returning from an interrupt routine. this must be handled by software. bit 76543210 $3f ($5f) i thsvnzcsreg read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000
21 atmega163(l) 1142e?avr?02/03 the stack pointer ? sp the atmega163 stack pointer is implemented as two 8-bit registers in the i/o space locations $3e ($5e) and $3d ($5d). as the atmega163 data memory has $460 loca- tions, 11 bits are used. the stack pointer points to the data sram stack area where the subroutine and inter- rupt stacks are located. this stack space in the data sram must be defined by the program before any subroutine calls are executed or interrupts are enabled. the stack pointer must be set to point above $60. the stack pointer is decremented by one when data is pushed onto the stack with the push instruction, and it is decremented by two when the return address is pushed onto the stack with subroutine call and interrupt. the stack pointer is incremented by one when data is popped from the stack with the pop instruction, and it is incremented by two when data is popped from the stack with return from subroutine ret or return from interrupt reti. reset and interrupt handling the atmega163 provides 17 different interrupt sources. these interrupts and the sepa- rate reset vector, each have a separate program vector in the program memory space. all interrupts are assigned individual enable bits which must be set (one) together with the i-bit in the status register in order to enable the interrupt. the lowest addresses in the program memory space are automatically defined as the reset and interrupt vectors. the complete list of vectors is shown in table 3. the list also determines the priority levels of the different interrupts. the lower the address the higher is the priority level. reset has the highest priority, and next is int0 ? the exter- nal interrupt request 0, etc. bit 151413121110 9 8 $3e ($5e) ? ? ? ? ? sp10 sp9 sp8 sph $3d ($5d) sp7 sp6 sp5 sp4 sp3 sp2 sp1 sp0 spl 76543210 read/write r r r r r r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000 00000000 table 3. reset and interrupt vectors vector no. program address source interrupt definition 1 $000 (1) reset external pin, power-on reset, brown-out reset and watchdog reset 2 $002 int0 external interrupt request 0 3 $004 int1 external interrupt request 1 4 $006 timer2 comp timer/counter2 compare match 5 $008 timer2 ovf timer/counter2 overflow 6 $00a timer1 capt timer/counter1 capture event 7 $00c timer1 compa timer/counter1 compare match a 8 $00e timer1 compb timer/counter1 compare match b 9 $010 timer1 ovf timer/counter1 overflow 10 $012 timer0 ovf timer/counter0 overflow 11 $014 spi, stc serial transfer complete 12 $016 uart, rxc uart, rx complete
22 atmega163(l) 1142e?avr?02/03 note: 1. when the bootrst fuse is programmed, the device will jump to the boot loader address at reset, see ?boot loader support? on page 134. the most typical and general program setup for the reset and interrupt vector addresses in atmega163 is: address labels code comments $000 jmp reset ; reset handler $002 jmp ext_int0 ; irq0 handler $004 jmp ext_int1 ; irq1 handler $006 jmp tim2_comp ; timer2 compare handler $008 jmp tim2_ovf ; timer2 overflow handler $00a jmp tim1_capt ; timer1 capture handler $00c jmp tim1_compa ; timer1 compare a handler $00e jmp tim1_compb ; timer1 compare b handler $010 jmp tim1_ovf ; timer1 overflow handler $012 jmp tim0_ovf ; timer0 overflow handler $014 jmp spi_stc ; spi transfer complete handler $016 jmp uart_rxc ; uart rx complete handler $018 jmp uart_dre ; udr empty handler $01a jmp uart_txc ; uart tx complete handler $01c jmp adc ; adc conversion complete interrupt handler $01e jmp ee_rdy ; eeprom ready handler $020 jmp ana_comp ; analog comparator handler $022 jmp twi ; two-wire serial interface interrupt handler ; $024 main: ldi r16,high(ramend) ; main program start $025 out sph,r16 ; set stack pointer to top of ram $026 ldi r16,low(ramend) $027 out spl,r16 ... ... ... 13 $018 uart, udre uart data register empty 14 $01a uart, txc uart, tx complete 15 $01c adc adc conversion complete 16 $01e ee_rdy eeprom ready 17 $020 ana_comp analog comparator 18 $022 twi two-wire serial interface table 3. reset and interrupt vectors (continued) vector no. program address source interrupt definition
23 atmega163(l) 1142e?avr?02/03 when the bootrst fuse is programmed and the boot section size set to 512 bytes, the most typical and general program setup for the reset and interrupt vector addresses in atmega163 is: address labels code comments $002 jmp ext_int0 ; irq0 handler ... ... ... $022 jmp twi ; two-wire serial interface interrupt handler ; $024 main: ldi r16,high(ramend) ; main program start $025 out sph,r16 ; set stack pointer to top of ram $026 ldi r16,low(ramend) $027 out spl,r16 $028 xxx ; .org $1f00 $1f00 jmp reset ; reset handler reset sources the atmega163 has four sources of reset:  power-on reset. the mcu is reset when the supply voltage is below the power-on reset threshold (v pot ).  external reset. the mcu is reset when a low level is present on the reset pin for more than 500 ns.  watchdog reset. the mcu is reset when the watchdog timer period expires and the watchdog is enabled.  brown-out reset. the mcu is reset when the supply voltage v cc is below the brown-out reset threshold (v bot ). during reset, all i/o registers are set to their initial values, and the program starts exe- cution from address $000 (unless the bootrst fuse is programmed, as explained above). the instruction placed in this address location must be a jmp ? absolute jump ? instruction to the reset handling routine. if the program never enables an interrupt source, the interrupt vectors are not used, and regular program code can be placed at these locations. the circuit diagram in figure 24 shows the reset logic. table 4 and table 5 define the timing and electrical parameters of the reset circuitry.
24 atmega163(l) 1142e?avr?02/03 figure 24. reset logic notes: 1. the power-on reset will not work unless the supply voltage has been below v pot (falling). table 4. reset characteristics (v cc = 5.0v) symbol parameter condition min typ max units v pot power-on reset threshold voltage (rising) 1.0 1.4 1.8 v power-on reset threshold voltage (falling) (1) 0.4 0.6 0.8 v v rst reset pin threshold voltage ? ? 0.85 v cc v v bot brown-out reset threshold voltage (bodlevel = 1) 2.4 2.7 3.2 v (bodlevel = 0) 3.5 4.0 4.5 mcu status register (mcusr) brown-out reset circuit boden bodlevel delay counters cksel[3:0] ck timeout wdrf borf extrf porf data b u s clock generator spike filter 100-500kw power-on reset circuit reset circuit watchdog timer reset on-chip rc oscillator counter reset internal reset vcc
25 atmega163(l) 1142e?avr?02/03 notes: 1. on power-up, the start-up time is increased with typ. 0.6 ms. 2. ?1? means unprogrammed, ?0? means programmed. 3. for possible clock selections, see ?clock options? on page 5. 4. when boden is programmed, add 100 s. 5. when boden is programmed, add 25 s. 6. default value. table 5 shows the start-up times from reset. when the cpu wakes up from power- down or power-save, only the clock counting part of the start-up time is used. the watchdog oscillator is used for timing the real time part of the start-up time. the number of wdt oscillator cycles used for each time-out is shown in table 6. the frequency of the watchdog oscillator is voltage dependent as shown in the electri- cal characteristics section. the device is shipped with cksel = ?0010? (int. rc oscillator, slowly rising power). table 5. reset delay selections (1) cksel (2) start-up time, v cc = 2.7v, bodlevel unprogrammed start-up time, v cc = 4.0v, bodlevel programmed recommended usage (3) 0000 4.2 ms + 6 ck 5.8 ms + 6 ck ext. clock, fast rising power 0001 30 s + 6 ck (4) 10 s + 6 ck (5) ext. clock, bod enabled 0010 (6) 67 ms + 6 ck 92 ms + 6 ck int. rc oscillator, slowly rising power 0011 4.2 ms + 6 ck 5.8 ms + 6 ck int. rc oscillator, fast rising power 0100 30 s + 6 ck (4) 10 s + 6 ck (5) int. rc oscillator, bod enabled 0101 67 ms + 6 ck 92 ms + 6 ck ext. rc oscillator, slowly rising power 0110 4.2 ms + 6 ck 5.8 ms + 6 ck ext. rc oscillator, fast rising power 0111 30 s + 6 ck (4) 10 s + 6 ck (5) ext. rc oscillator, bod enabled 1000 67ms + 32k ck 92 ms + 32k ck ext. low-frequency crystal 1001 67 ms + 1k ck 92 ms + 1k ck ext. low-frequency crystal 1010 67 ms + 16k ck 92 ms + 16k ck crystal oscillator, slowly rising power 1011 4.2 ms + 16k ck 5.8 ms + 16k ck crystal oscillator, fast rising power 1100 30 s + 16k ck (4) 10 s + 16k ck (5) crystal oscillator, bod enabled 1101 67 ms + 1k ck 92 ms + 1k ck ceramic resonator/ext. clock, slowly rising power 1110 4.2 ms + 1k ck 5.8 ms + 1k ck ceramic resonator, fast rising power 1111 30 s + 1k ck (4) 10 s + 1k ck (5) ceramic resonator, bod enabled
26 atmega163(l) 1142e?avr?02/03 note: 1. the bodlevel fuse can be used to select start-up times even if the brown-out detec- tion is disabled (boden fuse unprogrammed). power-on reset a power-on reset (por) pulse is generated by an on-chip detection circuit. the detec- tion level is defined in table 4. the por is activated whenever v cc is below the detection level. the por circuit can be used to trigger the start-up reset, as well as to detect a failure in supply voltage. a power-on reset (por) circuit ensures that the device is reset from power-on. reach- ing the power-on reset threshold voltage invokes a delay counter, which determines the delay, for which the device is kept in reset after v cc rise. the time-out period of the delay counter can be defined by the user through the cksel fuses. the different selections for the delay period are presented in table 5. the reset signal is activated again, without any delay, when the v cc decreases below detection level. figure 25. mcu start-up, reset tied to v cc . table 6. number of watchdog oscillator cycles (1) bodlevel v cc condition time-out number of cycles unprogrammed 2.7v 30 s 8 unprogrammed 2.7v 130 s 32 unprogrammed 2.7v 4.2 ms 1k unprogrammed 2.7v 67 ms 16k programmed 4.0v 10 s 8 programmed 4.0v 35 s 32 programmed 4.0v 5.8 ms 4k programmed 4.0v 92 ms 64k vcc reset time-out internal reset t tout v pot v rst
27 atmega163(l) 1142e?avr?02/03 figure 26. mcu start-up, reset extended externally external reset an external reset is generated by a low level on the reset pin. reset pulses longer than 500 ns will generate a reset, even if the clock is not running. shorter pulses are not guaranteed to generate a reset. when the applied signal reaches the reset threshold voltage ? v rst on its positive edge, the delay timer starts the mcu after the time-out period t tout has expired. figure 27. external reset during operation brown-out detection atmega163 has an on-chip brown-out detection (bod) circuit for monitoring the v cc level during the operation. the bod circuit can be enabled/disabled by the fuse boden. when the bod is enabled (boden programmed), and v cc decreases to a value below the trigger level, the brown-out reset is immediately activated. when v cc increases above the trigger level, the brown-out reset is deactivated after a delay. the delay is defined by the user in the same way as the delay of por signal, in table 5. the trigger level for the bod can be selected by the fuse bodlevel to be 2.7v (bodlevel unprogrammed), or 4.0v (bodlevel programmed). the trigger level has a hysteresis of 50 mv to ensure spike free brown-out detection. the bod circuit will only detect a drop in v cc if the voltage stays below the trigger level for longer than 9 s for trigger level 4.0v, 21 s for trigger level 2.7v (typical values). vcc reset time-out internal reset t tout v pot v rst
28 atmega163(l) 1142e?avr?02/03 figure 28. brown-out reset during operation the hysteresis on v bot : v bot+ = v bot + 25 mv, v bot- = v bot - 25 mv watchdog reset when the watchdog times ou t, it will generate a short reset pulse of 1 xtal cycle dura- tion. on the fa lling edge of this pulse, the delay time r starts counting the time-out period t tout . refer to page 60 for details on operation of the watchdog timer. figure 29. watchdog reset during operation mcu status register ? mcusr the mcu status register provides information on which reset source caused an mcu reset.  bits 7..4 ? res: reserved bits these bits are reserved bits in the atmega163 and always read as zero.  bit 3 ? wdrf: watchdog reset flag this bit is set if a watchdog reset occurs. the bit is reset by a power-on reset, or by writing a logic zero to the flag. vcc reset time-out internal reset v bot- v bot+ t tout 1 ck cycle bit 76543210 $34 ($54) ? ? ? ? wdrf borf extrf porf mcusr read/write r r r r r/w r/w r/w r/w initial value 0 0 0 0 see bit description
29 atmega163(l) 1142e?avr?02/03  bit 2 ? borf: brown-out reset flag this bit is set if a brown-out reset occurs. the bit is reset by a power-on reset, or by writing a logic zero to the flag.  bit 1 ? extrf: external reset flag this bit is set if an external reset occurs. the bit is reset by a power-on reset, or by writing a logic zero to the flag.  bit 0 ? porf: power-on reset flag this bit is set if a power-on reset occurs. the bit is reset only by writing a logic zero to the flag. to make use of the reset flags to identify a reset condition, the user should read and then reset the mcusr as early as possible in the program. if the register is cleared before another reset occurs, the source of the reset can be found by examining the reset flags. internal voltage reference atmega163 features an internal bandgap reference with a nominal voltage of 1.22v. this reference is used for brown-out detection, and it can be used as an input to the analog comparator and adc. the 2.56v reference to the adc is also generated from the internal bandgap reference. voltage reference enable signals and start-up time to save power, the reference is not always turned on. the reference is on during the fol- lowing situations: 1. when the bod is enabled (by programming the boden fuse) 2. when the bandgap reference is connected to the analog comparator (by setting the acbg bit in acsr). 3. when the adc is enabled. thus, when the bod is not enabled, after setting the acbg bit, the user must always allow the reference to start up before the output from the analog comparator is used. the bandgap reference uses typically 10 a, and to reduce power consumption in power-down mode, the user can avoid the three conditions above to ensure that the ref- erence is turned off before entering power-down mode. interrupt handling the atmega163 has two 8-bit interrupt mask control registers: gimsk ? general interrupt mask register and timsk ? timer/counter interrupt mask register. when an interrupt occurs, the global interrupt enable i-bit is cleared (zero) and all inter- rupts are disabled. the user software must set (one) the i-bit to enable nested interrupts. the i-bit is set (one) when a return from interrupt instruction ? reti ? is executed. when the program counter is vectored to the actual interrupt vector in order to execute the interrupt handling routine, hardware clears the corresponding flag that generated the interrupt. some of the interrupt flags can also be cleared by writing a logic one to the flag bit position(s) to be cleared. if an interrupt condition occurs when the corresponding interrupt enable bit is cleared (zero), the interrupt flag will be set and remembered until the interrupt is enabled, or the flag is cleared by software.
30 atmega163(l) 1142e?avr?02/03 if one or more interrupt conditions occur when the global interrupt enable bit is cleared (zero), the corresponding interrupt flag(s) will be set and remembered until the global interrupt enable bit is set (one), and will be executed by order of priority. note that external level interrupt does not have a flag, and will only be remembered for as long as the interrupt condition is present. note that the status register is not automatically stored when entering an interrupt rou- tine and restored when returning from an interrupt routine. this must be handled by software. interrupt response time the interrupt execution response for all the enabled avr interrupts is four clock cycles minimum. after four clock cycles the program vector address for the actual interrupt handling routine is executed. during this four clock cycle period, the program counter (13 bits) is pushed onto the stack. the vector is normally a jump to the interrupt routine, and this jump takes three clock cycles. if an interrupt occurs during execution of a multi- cycle instruction, this instruction is completed before the interrupt is served. if an inter- rupt occurs when the mcu is in sleep mode, the interrupt execution response time is increased by four clock cycles. a return from an interrupt handling routine takes four clock cycles. during these four clock cycles, the program counter (two bytes) is popped back from the stack, the stack pointer is incremented by two, and the i flag in sreg is set. when avr exits from an interrupt, it will always return to the ma in program and execute one more instruction before any pending interrupt is served. the general interrupt mask register ? gimsk  bit 7 ? int1: external interrupt request 1 enable when the int1 bit is set (one) and the i-bit in the status register (sreg) is set (one), the external pin interrupt is activated. the interrupt sense control1 bits 1/0 (isc11 and isc10) in the mcu general control register (mcucr) define whether the external interrupt is activated on rising and/or falling edge of the int1 pin or level sensed. activity on the pin will cause an interrupt request even if int1 is configured as an output. the corresponding interrupt of external interrupt request 1 is executed from program mem- ory address $004. see also ?external interrupts?.  bit 6 ? int0: external interrupt request 0 enable when the int0 bit is set (one) and the i-bit in the status register (sreg) is set (one), the external pin interrupt is activated. the interrupt sense control0 bits 1/0 (isc01 and isc00) in the mcu general control register (mcucr) define whether the external interrupt is activated on rising or falling edge of the int0 pin or level sensed. activity on the pin will cause an interrupt request even if int0 is configured as an output. the corre- sponding interrupt of external interrupt request 0 is executed from program memory address $002. see also ?external interrupts.?  bits 5 ? res: reserved bits this bit is reserved in the atmega163 and the read value is undefined. bit 7 6 5 4 3 2 1 0 $3b ($5b) int1 int0 ? ? ? ? ? ? gimsk read/write r/w r/w r r r r r r initial value 0 0 x 0 0 0 0 0
31 atmega163(l) 1142e?avr?02/03  bits 4..0 ? res: reserved bits these bits are reserved bits in the atmega163 and always read as zero. the general interrupt flag register ? gifr  bit 7 ? intf1: external interrupt flag1 when an edge on the int1 pin triggers an interrupt request, the correspnding interrupt flag, intf1, becomes set (one). if the i-bit in sreg and the corresponding interrupt enable bit, int1 in gimsk are set (one), the mcu will jump to the interrupt vector. the flag is cleared when the interrupt routine is executed. alternatively, the flag can be cleared by writing a logical one to it. this flag is always cleared when int1 is config- ured as a level interrupt.  bit 6 ? intf0: external interrupt flag0 when an event on the int0 pin triggers an interrupt request, the corresponding interrupt flag, intf0 becomes set (one). if the i-bit in sreg and the corresponding interrupt enable bit, int0 in gimsk are set (one), the mcu will jump to the interrupt vector. the flag is cleared when the interrupt routine is executed. alternatively, the flag can be cleared by writing a logical one to it. this flag is always cleared when int0 is config- ured as a level interrupt.  bits 5..0 ? res: reserved bits these bits are reserved bits in the atmega163 and always read as zero. the timer/counter interrupt mask register ? timsk  bit 7 ? ocie2: timer/counter2 output compare match interrupt enable when the ocie2 bit is set (one) and the i-bit in the status register is set (one), the timer/counter2 compare match interrupt is enabled. the corresponding interrupt (at vector $006) is executed if a compare match in timer/counter2 occurs, i.e., when the ocf2 bit is set in the timer/counter interrupt flag register ? tifr.  bit 6 ? toie2: timer/counter2 overflow interrupt enable when the toie2 bit is set (one) and the i-bit in the status register is set (one), the timer/counter2 overflow interrupt is enabled. the corresponding interrupt (at vector $008) is executed if an overflow in timer/counter2 occurs, i.e., when the tov2 bit is set in the timer/counter interrupt flag register ? tifr.  bit 5 ? ticie1: timer/counter1 input capture interrupt enable when the ticie1 bit is set (one) and the i-bit in the status register is set (one), the timer/counter1 input capture event interrupt is enabled. the corresponding interrupt bit 7 6 5 4 3 2 1 0 $3a ($5a) intf1 intf0 ? ? ? ? ? ? gifr read/write r/w r/w r r r r r r initial value 0 0 0 0 0 0 0 0 bit 7 6 5 4 3 2 1 0 $39 ($59) ocie2 toie2 ticie1 ocie1a ocie1b toie1 ? toie0 timsk read/write r/w r/w r/w r/w r/w r/w r r/w initial value 0 0 0 0 0 0 0 0
32 atmega163(l) 1142e?avr?02/03 (at vector $00a) is executed if a capture triggering event occurs on pd6 (icp), i.e., when the icf1 bit is set in the timer/counter interrupt flag register ? tifr.  bit 4 ? ocie1a: timer/counter1 output comparea match interrupt enable when the ocie1a bit is set (one) and the i-bit in the status register is set (one), the timer/counter1 compare a match interrupt is enabled. the corresponding interrupt (at vector $00c) is executed if a compare a match in timer/counter1 occurs, i.e., when the ocf1a bit is set in the timer/counter interrupt flag register ? tifr.  bit 3 ? ocie1b: timer/counter1 output compareb match interrupt enable when the ocie1b bit is set (one) and the i-bit in the status register is set (one), the timer/counter1 compare b match interrupt is enabled. the corresponding interrupt (at vector $00e) is executed if a compare b match in timer/counter1 occurs, i.e., when the ocf1b bit is set in the timer/counter interrupt flag register ? tifr.  bit 2 ? toie1: timer/counter1 overflow interrupt enable when the toie1 bit is set (one) and the i-bit in the status register is set (one), the timer/counter1 overflow interrupt is enabled. the corresponding interrupt (at vector $010) is executed if an overflow in timer/counter1 occurs, i.e., when the tov1 bit is set in the timer/counter interrupt flag register ? tifr.  bit 1 ? res: reserved bit this bit is a reserved bit in the atmega163 and always reads as zero.  bit 0 ? toie0: timer/counter0 overflow interrupt enable when the toie0 bit is set (one) and the i-bit in the status register is set (one), the timer/counter0 overflow interrupt is enabled. the corresponding interrupt (at vector $012) is executed if an overflow in timer/counter0 occurs, i.e., when the tov0 bit is set in the timer/counter interrupt flag register ? tifr. the timer/counter interrupt flag register ? tifr  bit 7 ? ocf2: output compare flag2 the ocf2 bit is set (one) when a compare match occurs between the timer/counter2 and the data in ocr2 ? output compare register2. ocf2 is cleared by hardware when executing the corresponding interrupt handling vector. alternatively, ocf2 is cleared by writing a logic one to the flag. when the i-bit in sreg, and ocie2 (timer/counter2 compare match interrupt enable), and the ocf2 are set (one), the timer/counter2 compare match interrupt is executed.  bit 6 ? tov2: timer/counter2 overflow flag the tov2 bit is set (one) when an overflow occurs in timer/counter2. tov2 is cleared by hardware when executing the correspondi ng interrupt handling vector. alterna- tively, tov2 is cleared by writing a logic one to the flag. when the sreg i-bit, and toie2 (timer/counter2 overflow interrupt enable), and tov2 are set (one), the bit 7 6 5 4 3 2 1 0 $38 ($58) ocf2 tov2 icf1 ocf1a ocf1b tov1 ? tov0 tifr read/write r/w r/w r/w r/w r/w r/w r r/w initial value 0 0 0 0 0 0 x 0
33 atmega163(l) 1142e?avr?02/03 timer/counter2 overflow interrupt is executed. in up/down pwm mode, this bit is set when timer/counter2 changes counting direction at $00.  bit 5 ? icf1: input capture flag1 the icf1 bit is set (one) to flag an input capture event, indicating that the timer/counter1 value has been transferred to the input capture register ? icr1. icf1 is cleared by hardware when executing the corresponding interrupt handling vector. alternatively, icf1 is cleared by writing a logic one to the flag.  bit 4 ? ocf1a: output compare flag 1a the ocf1a bit is set (one) when a compar e match occurs between the timer/counter1 and the data in ocr1a ? output compare register 1a. ocf1a is cleared by hardware when executing the corresponding interrupt handling vector. alternatively, ocf1a is cleared by writing a logic one to the flag. when the i-bit in sreg, and ocie1a (timer/counter1 compare match interrupt a enable), and the ocf1a are set (one), the timer/counter1a compare match interrupt is executed.  bit 3 ? ocf1b: output compare flag 1b the ocf1b bit is set (one) when a compar e match occurs between the timer/counter1 and the data in ocr1b ? output compare register 1b. ocf1b is cleared by hardware when executing the corresponding interrupt handling vector. alternatively, ocf1b is cleared by writing a logic one to the flag. when the i-bit in sreg, and ocie1b (timer/counter1 compare match interrupt b enable), and the ocf1b are set (one), the timer/counter1b compare match interrupt is executed.  bit 2 ? tov1: timer/counter1 overflow flag the tov1 is set (one) when an overflow occurs in timer/counter1. tov1 is cleared by hardware when executing the corresponding interrupt handling vector. alternatively, tov1 is cleared by writing a logic one to the flag. when the i-bit in sreg, and toie1 (timer/counter1 overflow interrupt enable), and tov1 are set (one), the timer/counter1 overflow interrupt is executed. in up/down pwm mode, this bit is set when timer/counter1 changes counting direction at $0000.  bit 1 ? res: reserved bit this bit is a reserved bit in the atmega163 and the read value is undefined.  bit 0 ? tov0: timer/counter0 overflow flag the bit tov0 is set (one) when an overflow occurs in timer/counter0. tov0 is cleared by hardware when executing the corresponding interrupt handling vector. alternatively, tov0 is cleared by writing a logic one to the flag. when the sreg i-bit, and toie0 (timer/counter0 overflow interrupt enable), and tov0 are set (one), the timer/counter0 overflow interrupt is executed. external interrupts the external interrupts are triggered by the int0 and int1 pins. observe that, if enabled, the interrupts will trigger even if the int0/int1 pins are configured as outputs. this feature provides a way of generating a software interrupt. the external interrupts can be triggered by a falling or rising edge or a low level. this is set up as indicated in the specification for the mcu control register ? mcucr. when the external interrupt is enabled and is configured as level triggered, the interrupt will trigger as long as the pin is held low.
34 atmega163(l) 1142e?avr?02/03 mcu control register ? mcucr the mcu control register contains control bits for general mcu functions.  bit 7 ? res: reserved bit this bit is a reserved bit in the atmega163 and always reads as zero.  bit 6 ? se: sleep enable the se bit must be set (one) to make the mcu enter the sleep mode when the sleep instruction is executed. to avoid the mcu entering the sleep mode unless it is the pro- grammers purpose, it is recommended to set the sleep enable se bit just before the execution of the sleep instruction.  bits 5, 4 ? sm1/sm0: sleep mode select bits 1 and 0 these bits select between the three available sleep modes as shown in table 7.  bits 3, 2 ? isc11, isc10: interrupt sense control 1 bit 1 and bit 0 the external interrupt 1 is activated by the external pin int1 if the sreg i-flag and the corresponding interrupt mask in the gimsk are set. the level and edges on the external int1 pin that activate the interrupt are defined in table 8. the value on the int1 pin is sampled before detecting edges. if edge or toggle interrupt is selected, pulses that last longer than one clock period will generate an interrupt. shorter pulses are not guaran- teed to generate an interrupt. if low level interrupt is selected, the low level must be held until the completion of the currently executing instruction to generate an interrupt. bit 76543210 $35 ($55) ? se sm1 sm0 isc11 isc10 isc01 isc00 mcucr read/write r r/w r/w r/w r/w r/w r/w r/w initial value00000000 table 7. sleep mode select sm1 sm0 sleep mode 00idle 0 1 adc noise reduction 1 0 power-down 11power-save table 8. interrupt 1 sense control isc11 isc10 description 0 0 the low level of int1 generates an interrupt request. 0 1 any logical change on int1 generates an interrupt request. 1 0 the falling edge of int1 generates an interrupt request. 1 1 the rising edge of int1 generates an interrupt request.
35 atmega163(l) 1142e?avr?02/03  bit 1, 0 ? isc01, isc00: interrupt sense control 0 bit 1 and bit 0 the external interrupt 0 is activated by the external pin int0 if the sreg i-flag and the corresponding interrupt mask are set. the level and edges on the external int0 pin that activate the interrupt are defined in table 9. the value on the int0 pin is sampled before detecting edges. if edge or toggle interrupt is selected, pulses that last longer than one clock period will generate an interrupt. shorter pulses are not guaranteed to generate an interrupt. if low level interrupt is selected, the low level must be held until the completion of the currently executing instruction to generate an interrupt. sleep modes to enter any of the four sleep modes, the se bit in mcucr must be set (one) and a sleep instruction must be executed. the sm1 and sm0 bits in the mcucr register select which sleep mode (idle, adc noise reduction, power-down, or power-save) will be activated by the sleep instruction. see table 7 for a summary. if an enabled inter- rupt occurs while the mcu is in a sleep mode, the mcu wakes up. the mcu is then halted for four cycles, executes the interrupt routine, and resumes execution from the instruction following sleep. the contents of the register file, sram, and i/o memory are unaltered when the device wakes up from sleep. if a reset occurs during sleep mode, the mcu wakes up and executes from the reset vector. idle mode when the sm1/sm0 bits are set to 00, the sleep instruction makes the mcu enter idle mode, stopping the cpu but allowing spi, uart, analog comparator, adc, two-wire serial interface, timer/counters, watchdog, and the interrupt system to continue oper- ating (if enabled). this enables the mcu to wake up from external triggered interrupts as well as internal ones like the timer overflow and uart receive complete interrupts. if wake-up from the analog comparator interrupt is not required, the analog comparator can be powered down by setting the acd bit in the analog comparator control and sta- tus register ? acsr. this will reduce power consumption in idle mode. if the adc is enabled, a conversion starts automatically when this mode is entered. adc noise reduction mode when the sm1/sm0 bits are set to 01, the sleep instruction makes the mcu enter adc noise reduction mode, stopping the cpu but allowing the adc, the external inter- rupts, the two-wire serial interface address watch, timer/counter2 and the watchdog to continue operating (if enabled). this improves the noise environment for the adc, enabling higher resolution measurements. if the adc is enabled, a conversion starts automatically when this mode is entered. apart from the adc conversion complete interrupt, only an external reset, a watchdog reset (if enabled), a brown-out reset, a two-wire serial interface address match interrupt, or an external level interrupt can wake up the mcu from adc noise reduction mode. a timer/counter2 output com- pare or overflow event will wake up the mcu, but will not generate an interrupt unless timer/counter2 is clocked asynchronously. in future devices this is subject to change. it is recommended for future code compatibil- ity to disable timer/counter2 interrupts during adc noise reduction mode if the timer/counter2 is clocked synchronously. table 9. interrupt 0 sense control isc01 isc00 description 0 0 the low level of int0 generates an interrupt request. 0 1 any logical change on int0 generates an interrupt request. 1 0 the falling edge of int0 generates an interrupt request. 1 1 the rising edge of int0 generates an interrupt request.
36 atmega163(l) 1142e?avr?02/03 power-down mode when the sm1/sm0 bits are 10, the sleep instruction makes the mcu enter power- down mode. in this mode, the external oscillator is stopped, while the external inter- rupts, the two-wire serial interface address match, and the watchdog continue operating (if enabled). only an external reset, a watchdog reset, a brown-out reset, a two-wire serial interface address match interrupt, or an external level interrupt can wake up the mcu. note that if a level triggered interrupt is used for wake-up from power-down mode, the changed level must be held for some time to wake up the mcu. this makes the mcu less sensitive to noise. the changed level is sampled twice by the watchdog oscillator clock, and if the input has the required leve l during this time, the mcu will wake up. the period of the watchdog oscillator is 1 s (nominal) at 5.0v and 25 c. the frequency of the watchdog oscillator is voltage dependent as shown in the electrical characteristics section. when waking up from power-down mode, there is a delay from the wake-up condition occurs until the wake-up becomes effective. this allows the clock to restart and become stable after having been stopped. the wake-up period is defined by the same cksel fuses that define the reset time-out period, as seen in table 5 on page 25. power-save mode when the sm1/sm0 bits are 11, the sleep instruction forces the mcu into the power- save mode. this mode is identical to power-down, with one exception: if timer/counter2 is clocked asynchronously, i.e., the as2 bit in assr is set, timer/counter2 will run during sleep. the device can wake up from either timer over- flow or output compare event from timer/counter2 if the corresponding timer/counter2 interrupt enable bits are set in timsk, and the global interrupt enable bit in sreg is set. if the asynchronous timer is not clocked asynchronously, power-down mode is recom- mended instead of power-save mode because the contents of the registers in the asynchronous timer should be considered undefined after wake-up in power-save mode if as2 is 0.
37 atmega163(l) 1142e?avr?02/03 calibrated internal rc oscillator the calibrated internal oscillator provides a fixed 1 mhz (nominal) clock at 5v and 25 c. this clock may be used as the system clock. see the section ?clock options? on page 5 for information on how to select this clock as the system clock. this oscillator can be calibrated by writing the calibration byte to the osccal register. when this oscillator is used as the chip clock, the watchdog oscillator will still be used for the watchdog timer and for the reset time-out. at 5v and 25 o c, the pre-programmed cali- bration byte gives a frequency within 1% of the nominal frequency. for details on how to use the pre-programmed calibration value, see ?calibration byte? on page 144. oscillator calibration register ? osccal  bits 7..0 ? cal7..0: oscillator calibration value writing the calibration byte to this address will trim the internal oscillator to remove pro- cess variations from the oscillator frequency. when osccal is zero, the lowest available frequency is chosen. writing non-zero values to this register will increase the frequency of the internal oscillator. writing $ff to the register gives the highest avail- able frequency. the calibrated oscillator is used to time eeprom and flash access. if eeprom or flash is written, do not calibrate to more than 10% above the nominal frequency. other- wise, the eeprom or flash write operation may fail. note that the oscillator is intended for calibration to 1.0mhz, thus tuning to other values is not guaranteed. special function i/o register ? sfior  bit 7..4 ? res: reserved bits these bits are reserved bits in the atmega163 and always read as zero.  bit 3 ? acme: analog comparator multiplexer enable when this bit is set (one) and the adc is switched off (aden in adcsr is zero), the adc multiplexer selects the negative input to the analog comparator. when this bit is cleared (zero), ain1 is applied to the negative input of the analog comparator. for a detailed description of this bit, see ?analog comparator multiplexed input? on page 104. bit 76543210 $31 ($51) cal7 cal6 cal5 cal4 cal3 cal2 cal1 cal0 read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000 table 10. internal rc oscillator frequency range. osccal value min frequency (mhz) max frequency (mhz) $00 0.5 1.0 $7f 0.7 1.5 $ff 1.0 2.0 bit 7 6 5 4 3 2 1 0 $30 ($50) ? ? ? ? acme pud psr2 psr10 sfior read/write r r r r r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0
38 atmega163(l) 1142e?avr?02/03  bit 2 ? pud: pull-up disable when this bit is set (one), all pull-ups on all ports are disabled. if the bit is cleared (zero), the pull-ups can be individually enabled as described in the chapter ?i/o ports? on page 115.  bit 1 ? psr2: prescaler reset timer/counter2 when this bit is set (one) the timer/counter 2 prescaler will be reset. the bit will be cleared by hardware after the operation is performed. writing a zero to this bit will have no effect. this bit will always be read as zero if timer/counter2 is clocked by the internal cpu clock. if this bit is written when timer/counter2 is operating in asynchronous mode. the bit will remain one until the prescaler has been reset. see ?asynchronous operation of timer/counter2? on page 58 for a detailed description of asynchronous operation.  bit 0 ? psr10: prescaler reset timer/counter1 and timer/counter0 when this bit is set (one) the timer/c ounter1 and timer/count er0 prescaler will be reset. the bit will be cleared by hardware after the operation is performed. writing a zero to this bit will have no effect. note that timer/counter1 and timer/counter0 share the same prescaler and a reset of this prescaler will affect both timers. this bit will always be read as zero.
39 atmega163(l) 1142e?avr?02/03 timer/counters the atmega163 provides three general purpose timer/counters ? two 8-bit t/cs and one 16-bit t/c. timer/counter2 can optionally be asynchronously clocked from an exter- nal oscillator. this oscillator is optimized for use with a 32.768 khz watch crystal, enabling use of timer/counter2 as a real time clock (rtc). timer/counter0 and timer/counter1 have individual prescaling selection from the same 10-bit prescaler. timer/counter2 has its own prescaler. both these prescalers can be reset by setting the corresponding control bits in the special functions i/o register (sfior). these timer/counters can either be used as a timer with an internal clock time-base or as a counter with an external pin connection which triggers the counting. timer/counter prescalers figure 30. prescaler for timer/counter0 and timer/counter1 for timer/counter0 and timer/counter1, the four different prescaled selections are: ck/8, ck/64, ck/256, and ck/1024, where ck is the oscillator clock. for the two timer/counter0 and timer/counter1, ck, external source, and stop can also be selected as clock sources. setting the psr10 bit in sfior resets the prescaler. this allows the user to operate with a predictable prescaler. note that timer/counter1 and timer/counter0 share the same prescale r and a prescaler rese t will affect both timer/counters. psr10 clear tck1 tck0
40 atmega163(l) 1142e?avr?02/03 figure 31. prescaler for timer/counter2 the clock source for timer/counter2 is named pck2. pck2 is by default connected to the main system clock ck. by setting the as2 bit in assr, timer/counter2 is asynchro- nously clocked from the pc6(tosc1) pin. this enables use of timer/counter2 as a real time clock (rtc). when as2 is set, pins pc6(tosc1) and pc7(tosc2) are dis- connected from port c. a crystal can then be connected between the pc6(tosc1) and pc7(tosc2) pins to serve as an independent clock source for timer/counter2. the oscillator is optimized for use with a 32. 768 khz crystal. applying an external clock source to tosc1 is not recommended. setting the psr2 bit in sfior resets the pres- caler. this allows the user to operate with a predictable prescaler. 8-bit timer/counter0 figure 32 shows the block diagram for timer/counter0. the 8-bit timer/counter0 can select clock source from ck, prescaled ck, or an external pin. in addition it can be stopped as described in ?timer/counter0 control register ? tccr0? on page 41. the overflow status flag is found in ?the timer/counter interrupt flag register ? tifr? on page 32. control signals are found in the timer/counter0 con- trol register ? tccr0. the interrupt enable/disable settings for timer/counter0 are found in ?the timer/counter interrupt mask register ? timsk? on page 31. when timer/counter0 is externally clocked, the external signal is synchronized with the oscillator frequency of the cpu. to assure proper sampling of the external clock, the minimum time between two external clock transitions must be at least one internal cpu clock period. the external clock signal is sampled on the rising edge of the internal cpu clock. the 8-bit timer/counter0 features both a high resolution and a high accuracy usage with the lower prescaling opportunities. similarly, the high prescaling opportunities make the timer/counter0 useful for lower speed functions or exact timing functions with infre- quent actions. 10-bit t/c prescaler timer/counter2 clock source ck pck2 tosc1 as2 cs20 cs21 cs22 pck2/8 pck2/64 pck2/128 pck2/1024 pck2/256 pck2/32 0 psr2 clear tck2
41 atmega163(l) 1142e?avr?02/03 figure 32. timer/counter0 block diagram timer/counter0 control register ? tccr0  bits 7..3 ? res: reserved bits these bits are reserved bits in the atmega163 and always read as zero.  bits 2..0 ? cs02, cs01, cs00: clock select0, bit 2, 1, and 0 the clock select0 bits 2,1, and 0 define the prescaling source of timer0. the stop condition provides a timer enable/disable function. the prescaled ck modes are scaled directly from the ck oscillator clock. if the external pin modes are used for timer/counter0, transitions on pb0/(t0) will clock the counter even if the pin is config- ured as an output. this feature can give the user sw control of the counting. 8-bit da ta bus t/c0 control register (tccr0) timer/counter0 (tcnt0) 0 7 t/c clk source control logic cs02 cs01 cs00 ck t/c0 over- flow irq timer int. mask register (timsk) toie0 toie1 ocie1a ocie1b ticie1 toie2 ocie2 timer int. flag register (tifr) tov0 tov1 ocf1a ocf1b icf1 tov2 ocf2 bit 76543210 $33 ($53) ?????cs02cs01cs00t ccr0 read/write rrrrrr/wr/wr/w initial value00000000 table 11. clock0 prescale select cs02 cs01 cs00 description 0 0 0 stop, timer/counter0 is stopped. 001ck 010ck/8 011ck/64 100ck/256 101ck/1024 1 1 0 external pin t0, falling edge 1 1 1 external pin t0, rising edge
42 atmega163(l) 1142e?avr?02/03 timer/counter 0 ? tcnt0 the timer/counter0 is implemented as an up-counter with read and write access. if the timer/counter0 is written and a clock source is present, the timer/counter0 continues counting in the clock cycle following the write operation. 16-bit timer/counter1 figure 33 shows the block diagram for timer/counter1. figure 33. timer/counter1 block diagram the 16-bit timer/counter1 can select clock source from ck, prescaled ck, or an exter- nal pin. in addition it can be stopped as described in section ?timer/counter1 control register b ? tccr1b? on page 45. the diff erent status flags (overflow, compare match, and capture event) are found in the timer/counter interrupt flag register ? tifr. control signals are found in the timer/counter1 control registers ? tccr1a and tccr1b. the interrupt enable/disable setti ngs for timer/counter1 are found in the timer/counter interrupt mask register ? timsk. when timer/counter1 is externally clocked, the external signal is synchronized with the oscillator frequency of the cpu. to assure proper sampling of the external clock, the minimum time between two external clock transitions must be at least one internal cpu bit 76543210 $34 ($54) msb lsb tcnt0 read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000 8-bit da ta bus t/c1 control register b (tccr1b) t/c1 control register a (tccr1a) t/c1 input capture register (icr1) 16 bit comparator 16 bit comparator timer/counter1 output compare register a timer/counter1 output compare register b timer/counter1 (tcnt1) timer int. flag register (tifr) 0 0 0 0 0 0 7 7 7 7 7 7 8 8 8 8 8 8 15 15 15 15 15 15 control logic com1a1 com1b1 cs12 tov1 tov1 tov0 ocf1a ocf1a ocf1b ocf1b icf1 icf1 com1a0 com1b0 cs1 1 ctc1 pwm1 1 pwm10 ices1 icnc1 cs10 ck t/c1 compare match a irq t/c1 compare match b irq t/c1 input capture irq t/c1 over- flow irq capture trigger t/c clock source t/c clear up/down timer int. mask register (timsk) toie0 toie1 ocie1a ocie1b ticie1 toie2 tov2 ocie2 ocf2 foc1a foc1b t1
43 atmega163(l) 1142e?avr?02/03 clock period. the external clock signal is sampled on the rising edge of the internal cpu clock. the 16-bit timer/counter1 features both a high resolution and a high accuracy usage with the lower prescaling opportunities. similarly, the high prescaling opportunities makes the timer/counter1 useful for lower speed functions or exact timing functions with infrequent actions. the timer/counter1 supports two output compare functions using the output compare register 1 a and b (ocr1a and ocr1b) as the data sources to be compared to the timer/counter1 contents. the output compare functions includes optional clearing of the counter on compare a match, and actions on the output compare pins on both compare matches. timer/counter1 can also be used as an 8-, 9-, or 10-bit pulse width modulator (pwm). in this mode the counter and the ocr1a/ocr1b registers serve as a dual glitch-free stand-alone pwm with centered pulses. alternatively, the timer/counter1 can be con- figured to operate at twice the speed in pwm mode, but without centered pulses. refer to page 48 for a detailed description of this function. the input capture function of timer/counter1 provides a capture of the timer/counter1 contents to the input capture register ? icr1, triggered by an external event on the input capture pin ? icp. the actual capture event settings are defined by the timer/counter1 control register ? tccr1b. in addition, the analog comparator can be set to trigger the input capture. refer to the section, ?the analog comparator? on page 102, for details on this. the icp pin logic is shown in figure 34. figure 34. icp pin schematic diagram if the noise canceler function is enabled, the actual trigger condition for the capture event is monitored over four samples, and all four must be equal to activate the capture flag.
44 atmega163(l) 1142e?avr?02/03 timer/counter1 control register a ? tccr1a  bits 7, 6 ? com1a1, com1a0: compare output mode1a, bits 1, and 0 the com1a1 and com1a0 control bits determine any output pin action following a compare match in timer/counter1. any output pin actions affect pin oc1a ? output compare a. this is an alternative function to an i/o port, and the corresponding direc- tion control bit must be set (one) to control an output pin. the control configuration is shown in table 10.  bits 5, 4 ? com1b1, com1b0: compare output mode1b, bits 1, and 0 the com1b1 and com1b0 control bits determine any output pin action following a compare match in timer/counter1. any output pin actions affect pin oc1b ? output compare b. this is an alternative function to an i/o port, and the corresponding direc- tion control bit must be set (one) to control an output pin. the control configuration is shown in table 10. note: 1. x = a or b. in pwm mode, these bits have a different function. refer to table 14 for a detailed description.  bit 3 ? foc1a: force output compare1a writing a logical one to this bit, forces a change in the compare match output pin pd5 according to the values already set in com1a1 and com1a0. if the com1a1 and com1a0 bits are written in the same cycle as foc1a, the new settings will not take effect until next compare match or forced compare match occurs. the force output compare bit can be used to change the output pin without waiting for a compare match in the timer. the automatic action programmed in com1a1 and com1a0 happens as if a compare match had occurred, but no interrupt is generated and it will not clear the timer even if ctc1 in tccr1b is set. the corresponding i/o pin must be set as an out- put pin for the foc1a bit to have effect on the pin. the foc1a bit will always be read as zero. the setting of the foc1a bit has no effect in pwm mode.  bit 2 ? foc1b: force output compare1b writing a logical one to this bit, forces a change in the compare match output pin pd4 according to the values already set in com1b1 and com1b0. if the com1b1 and com1b0 bits are written in the same cycle as foc1b, the new settings will not take effect until next compare match or forced compare match occurs. the force output compare bit can be used to change the output pin without waiting for a compare match bit 7 6 5 4 3210 $2f ($4f) com1a1 com1a0 com1b1 com1b0 foc1a foc1b pwm11 pwm10 tccr1a read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 table 12. compare 1 mode select (1) com1x1 com1x0 description 0 0 timer/counter1 disconnected from output pin oc1x 0 1 toggle the oc1x output line. 1 0 clear the oc1x output line (to zero). 1 1 set the oc1x output line (to one).
45 atmega163(l) 1142e?avr?02/03 in the timer. the automatic action programmed in com1b1 and com1b0 happens as if a compare match had occurred, but no interrupt is generated. the corresponding i/o pin must be set as an output pin for the foc1b bit to have effect on the pin. the foc1b bit will always be read as zero. the setting of the foc1b bit has no effect in pwm mode.  bits 1..0 ? pwm11, pwm10: pulse width modulator select bits these bits select pwm operation of timer/counter1 as specified in table 11. this mode is described on page 48. timer/counter1 control register b ? tccr1b  bit 7 ? icnc1: input capture1 noise canceler (4 cks) when the icnc1 bit is cleared (zero), the i nput capture trigger no ise canceler function is disabled. the input capture is triggered at the first rising/fa lling edge samp led on the icp ? input capture pin ? as specified. when the icnc1 bit is set (one), four successive samples are measures on the icp ? input capture pin, and all samples must be high/low according to the input capture trigger specification in the ices1 bit. the actual sampling frequency is xtal clock frequency.  bit 6 ? ices1: input capture1 edge select while the ices1 bit is cleared (zero), the timer/counter1 contents are transferred to the input capture register ? icr1 ? on the fa lling edge of the input capture pin ? icp. while the ices1 bit is set (one), the timer/counter1 contents are transferred to the input capture register ? icr1 ? on the rising edge of the input capture pin ? icp.  bits 5, 4 ? res: reserved bits these bits are reserved bits in the atmega163 and always read as zero.  bit 3 ? ctc1: clear timer/counter1 on compare match when the ctc1 control bit is set (one), the timer/counter1 is reset to $0000 in the clock cycle after a compare a match. if the ctc1 control bit is cleared, timer/counter1 continues counting and is unaffected by a compare match. when a prescaling of 1 is used, and the compare a register is set to c, the timer will count as follows if ctc1 is set: ... | c-1 | c | 0 | 1 |... table 13. pwm mode select pwm11 pwm10 description 0 0 pwm operation of timer/counter1 is disabled 0 1 timer/counter1 is an 8-bit pwm 1 0 timer/counter1 is a 9-bit pwm 1 1 timer/counter1 is a 10-bit pwm bit 7654 3210 $2e ($4e) icnc1 ices1 ? ? ctc1 cs12 cs11 cs10 tccr1b read/write r/w r/w r r r/w r/w r/w r/w initial value0000 0000
46 atmega163(l) 1142e?avr?02/03 when the prescaler is set to divide by eight, the timer will count like this: ... | c-1, c-1, c-1, c-1, c-1, c-1, c-1, c-1 | c, c, c, c, c, c, c, c | 0, 0, 0, 0, 0, 0, 0, 0 |1,1,1,1,1,1,1,1|... in pwm mode, this bit has a different function. if the ctc1 bit is cleared in pwm mode, the timer/counter1 acts as an up/down counter. if the ctc1 bit is set (one), the timer/counter wraps when it reaches the top value. refer to page 48 for a detailed description.  bits 2..0 ? cs12, cs11, cs10: clock select1, bit 2, 1, and 0 the clock select1 bits 2, 1, and 0 define the prescaling source of timer/counter1. the stop condition provides a timer enable/disable function. the prescaled modes are scaled directly from the ck oscillator clock. if the external pin modes are used for timer/counter1, transitions on pb1/(t1) will clock the counter even if the pin is config- ured as an output. this feature can give the user sw control of the counting. timer/counter1 ? tcnt1h and tcnt1l this 16-bit register contains the prescaled value of the 16-bit timer/counter1. to ensure that both the high and low bytes are read and written simultaneously when the cpu accesses these registers, the access is performed using an 8-bit temporary regis- ter (temp). this temporary register is also used when accessing ocr1a, ocr1b, and icr1. if the main program and also interrupt routines perform access to registers using temp, interrupts must be disabled during access from the main program and interrupt routines. table 14. clock 1 prescale select cs12 cs11 cs10 description 0 0 0 stop, the timer/counter1 is stopped. 001ck 010ck/8 011ck/64 100ck/256 1 0 1 ck/1024 1 1 0 external pin t1, falling edge 1 1 1 external pin t1, rising edge bit 151413121110 9 8 $2d ($4d) msb tcnt1h $2c ($4c) lsb tcnt1l 76543210 read/write r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000 00000000
47 atmega163(l) 1142e?avr?02/03 tcnt1 timer/counter1 write when the cpu writes to the high byte tcnt1h, the written data is placed in the temp register. next, when the cpu writes the low byte tcnt1l, this byte of data is combined with the byte data in the temp register, and all 16 bits are written to the tcnt1 timer/counter1 register simultaneously. consequently, the high byte tcnt1h must be accessed first for a full 16-bit register write operation. tcnt1 timer/counter1 read when the cpu reads the low byte tcnt1l, the data of the low byte tcnt1l is sent to the cpu and the data of the high byte tcnt1h is placed in the temp register. when the cpu reads the data in the high byte tcnt1h, the cpu receives the data in the temp register. consequently, the low byte tcnt1l must be accessed first for a full 16-bit register read operation. the timer/counter1 is realized as an up or up/down (in pwm mode) counter with read and write access. if timer/counter1 is written to and a clock source is selected, the timer/counter1 continues counting in the timer clock cycle after it is preset with the writ- ten value. timer/counter1 output compare register ? ocr1ah and ocr1al timer/counter1 output compare register ? ocr1bh and ocr1bl the output compare registers are 16-bit read/write registers. the timer/counter1 output compare registers contain the data to be continuously compared with timer/counter1. actions on compare matches are specified in the timer/counter1 control and status register. a software write to the timer/counter register blocks compare matches in the next timer/counter clock cycle. this prevents immediate interrupts when initializing the timer/counter. a compare match will set the compare interrupt flag in the cpu clock cycle following the compare event. since the output compare registers ? ocr1 a and ocr1b ? are 16-bit registers, a temporary register temp is used when ocr1a/b are written to ensure that both bytes are updated simultaneously. when the cpu writes the high byte, ocr1ah or ocr1bh, the data is temporarily stored in the temp register. when the cpu writes the low byte, ocr1al or ocr1bl, the temp register is simultaneously written to ocr1ah or ocr1bh. consequently, the high byte ocr1ah or ocr1bh must be written first for a full 16-bit register write operation. bit 151413121110 9 8 $2b ($4b) msb ocr1ah $2a ($4a) lsb ocr1al 76543210 read/write r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000 00000000 bit 151413121110 9 8 $29 ($49) msb ocr1bh $28 ($48) lsb ocr1bl 76543210 read/write r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000 00000000
48 atmega163(l) 1142e?avr?02/03 the temp register is also used when accessing tcnt1 and icr1. if the main program and also interrupt routines perform access to registers using temp, interrupts must be disabled during access from the main program and interrupt routines. timer/counter1 input capture register ? icr1h and icr1l the input capture register is a 16-bit read-only register. when the rising or falling edge (according to the input capture edge setting ? ices1) of the signal at the input capture pin ? icp ? is detected, the current value of the timer/counter1 register ? tcnt1 ? is transferred to the input capture register ? icr1. at the same time, the input capture flag ? icf1 ? is set (one). since the input capture register ? icr1 ? is a 16-bit register, a temporary register temp is used when icr1 is read to ensure that both bytes are read simultaneously. when the cpu reads the low byte icr1l, the data is sent to the cpu and the data of the high byte icr1h is placed in the temp register. when the cpu reads the data in the high byte icr1h, the cpu receives the data in the temp register. consequently, the low byte icr1l must be accessed first for a full 16-bit register read operation. the temp register is also used when accessing tcnt1, ocr1a, and ocr1b. if the main program and also interrupt routines accesses registers using temp, interrupts must be disabled during access from the main program and interrupt routines. timer/counter1 in pwm mode when the pwm mode is selected, timer/counter1 and the output compare register1a ? ocr1a and the output compare register1b ? ocr1b, form a dual 8,- 9-, or 10-bit, free-running, glitch-free, and phase correct pwm with outputs on the pd5 (oc1a) and pd4(oc1b) pins. in this mode, the timer/counter1 acts as an up/down counter, count- ing up from $0000 to top (see table 16), where it turns and counts down again to zero before the cycle is repeated. when the counter value matches the contents of the 8, 9, or 10 least significant bits (depending on resolution) of ocr1a or ocr1b, the pd5(oc1a)/pd4(oc1b) pins are set or cleared according to the settings of the com1a1/com1a0 or com1b1/com1b0 bits in the timer/counter1 control register tccr1a. refer to table 12 on page 44 for details. alternatively, the timer/counter1 can be configured to a pwm that operates at twice the speed as in the mode described above. then the timer/counter1 and the output com- pare register1a ? ocr1a and the output compare register1b ? ocr1b, form a dual 8-, 9-, or 10-bit, free-running and glitch-free pwm with outputs on the pd5(oc1a) and pd4(oc1b) pins. bit 151413121110 9 8 $27 ($47) msb icr1h $26 ($46) lsb icr1l 76543210 read/write r r rrrrrr rrrrrrrr initial value00000000 00000000
49 atmega163(l) 1142e?avr?02/03 as shown in table 15, the pwm operates at either 8, 9, or 10 bits resolution. note the unused bits in ocr1a, ocr1b, and tcnt1 will automatically be written to zero by hardware. for example, bit 9 to 15 will be set to zero in ocr1a, ocr1b, and tcnt1 if the 9-bit pwm resolution is selected. this makes it possible for the user to perform read- modify-write operations in any of the three resolution modes and the unused bits will be treated as don?t care. note: 1. x = a or b note that in the pwm mode, the 8, 9, or 10 least significant ocr1a/ocr1b bits (depending on resolution), when written, are transferred to a temporary location. they are latched when timer/counter1 reaches the value top. this prevents the occurrence of odd-length pwm pulses (glitches) in the event of an unsynchronized ocr1a/ocr1b write. see figure 35 and figure 36 for an example in each mode. table 15. timer top values and pwm frequency ctc1 pwm11 pwm10 pwm resolution timer top value frequency 0 0 1 8-bit $00ff (255) f tck1 /510 0 1 0 9-bit $01ff (511) f tck1 /1022 0 1 1 10-bit $03ff(1023) f tck1 /2046 1 0 1 8-bit $00ff (255) f tck1 /256 1 1 0 9-bit $01ff (511) f tck1 /512 1 1 1 10-bit $03ff(1023) f tck1 /1024 table 16. timer top values and pwm frequency pwm resolution timer top value frequency 8-bit $00ff (255) f tc1 /510 9-bit $01ff (511) f tc1 /1022 10-bit $03ff(1023) f tc1 /2046 table 17. compare1 mode select in pwm mode (1) ctc1 com1x 1 com1x 0 effect on ocx1 0 0 0 not connected 0 0 1 not connected 01 0 cleared on compare match, up-counting. set on compare match, down-counting (non-inverted pwm). 01 1 cleared on compare match, down-counting. set on compare match, up-counting (inverted pwm). 1 0 0 not connected 1 0 1 not connected 1 1 0 cleared on compare match, set on overflow. 1 1 1 set on compare match, cleared on overflow.
50 atmega163(l) 1142e?avr?02/03 figure 35. effects of unsynchronized ocr1 latching. figure 36. effects of unsynchronized ocr1 latching in overflow mode. during the time between the write and the latch operation, a read from ocr1a or ocr1b will read the contents of the temporary location. this means that the most recently written value always will read out of ocr1a/b. when the ocr1x contains $0000 or top, and the up/down pwm mode is selected, the output oc1a/oc1b is updated to low or high on the next compare match according to the settings of com1a1/com1a0 or com1b1/com1b0. this is shown in table 18. in overflow pwm mode, the output oc1a/oc1b is held low or high only when the output compare register contains top. note: 1. x = a or b table 18. pwm outputs ocr1x = $0000 or top (1) com1x1 com1x0 ocr1x output oc1x 1 0 $0000 l 10top h 1 1 $0000 h 11top l pwm output oc1x pwm output oc1x unsynchronized oc1x latch synchronized oc1x latch note: x = a or b pwm output oc1x pwm output oc1x unsynchronized oc1x latch synchronized oc1x latch note: x = a or b
51 atmega163(l) 1142e?avr?02/03 in overflow pwm mode, the table above is only valid for ocr1x = top. in pwm mode, the timer overflow flag1, tov1, is set when the counter advances from $0000. in overflow pwm mode, the timer overflow flag is set as in normal timer/counter mode. timer overflow interrupt1 operates exactly as in normal timer/counter mode, i.e., it is executed when tov1 is set provided that timer overflow interrupt1 and global interrupts are enabled. this also applies to the timer output compare1 flags and interrupts. 8-bit timer/counter 2 figure 37 shows the block diagram for timer/counter2. figure 37. timer/counter2 block diagram the 8-bit timer/counter2 can select clock source from ck, prescaled ck, or external crystal input tosc1. it can also be stopped as described in the section ?timer/counter2 control register ? tccr2? on page 52. the status flags (overflow and compare match) are found in the timer/counter inter- rupt flag register ? tifr. control signals are found in the timer/counter control register tccr2. the interrupt enable/disable settings are found in ?the timer/counter interrupt mask register ? timsk? on page 31. when timer/counter2 is externally clocked, the external signal is synchronized with the oscillator frequency of the cpu. to assure proper sampling of the external clock, the minimum time between two external clock transitions must be at least one internal cpu 8-bit data bus 8-bit asynch t/c2 data bus asynch. status register (assr) timer int. flag register (tifr) timer/counter2 (tcnt2) synch unit 8-bit comparator output compare register2 (ocr2) timer int. mask register (timsk) 0 0 0 7 7 7 t/c clk source up/down t/c clear control logic tov0 tov1 ocf1b ocf1a icf1 tov2 ocf2 ocf2 tov2 toie0 toie1 ocie1a ocie1b ticie1 toie2 ocie2 ocr2ub tc2ub icr2ub ck ck pck2 t/c2 over- flow irq t/c2 compare match irq t/c2 control register (tccr2) cs22 com21 pwm2 as2 cs21 com20 cs20 ctc2 foc2 tosc1 psr2
52 atmega163(l) 1142e?avr?02/03 clock period. the external clock signal is sampled on the rising edge of the internal cpu clock. this module features a high resolution and a high accuracy usage with the lower pres- caling opportunities. similarly, the high prescaling opportunities make this unit useful for lower speed functions or exact timing functions with infrequent actions. timer/counter2 can also be used as an 8-bit pulse width modulator. in this mode, timer/counter2 and the output compare register serve as a glitch-free, stand-alone pwm with centered pulses. refer to page 57 for a detailed description on this function. timer/counter2 control register ? tccr2  bit 7 ? foc2: force output compare writing a logical one to this bit, forces a change in the compare match output pin pd7 (oc2) according to the values already set in com21 and com20. if the com21 and com20 bits are written in the same cycle as foc2, the new settings will not take effect until next compare match or forced output compare match occurs. the force output compare bit can be used to change the output pin without waiting for a compare match in the timer. the automatic action programmed in com21 and com20 happens as if a compare match had occurred, but no interrupt is generated, and the timer/counter will not be cleared even if ctc2 is set. the corresponding i/o pin must be set as an output pin for the foc2 bit to have effect on the pin. the foc2 bit will always be read as zero. setting the foc2 bit has no effect in pwm mode.  bit 6 ? pwm2: pulse width modulator enable when set (one) this bit enables pwm mode for timer/counter2. this mode is described on page 43.  bits 5, 4 ? com21, com20: compare output mode, bits 1 and 0 the com21 and com20 control bits determine any output pin action following a com- pare match in timer/counter2. output pin actions affect pin pd7(oc2). this is an alternative function to an i/o port, and the corresponding direction control bit must be set (one) to control an output pin. the control configuration is shown in table 19. note: 1. in pwm mode, these bits have a different function. refer to table 21 on page 55 for a detailed description. bit 7 6 5 4 3 2 1 0 $25 ($45) foc2 pwm2 com21 com20 ctc2 cs22 cs21 cs20 tccr2 read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value 0 0 0 0 0 0 0 0 table 19. compare mode select (1) com21 com20 description 0 0 timer/counter disconnected from output pin oc2 0 1 toggle the oc2 output line. 1 0 clear the oc2 output line (to zero). 1 1 set the oc2 output line (to one).
53 atmega163(l) 1142e?avr?02/03  bit 3 ? ctc2: clear timer/counter on compare match when the ctc2 control bit is set (one), timer/counter2 is reset to $00 in the cpu clock cycle following a compare match. if the control bit is cleared, the timer/counter2 contin- ues counting and is unaffected by a compare match. when a prescaling of 1 is used, and the compare register is set to c, the timer will count as follows if ctc2 is set: ... | c-1 | c | 0 | 1 |... when the prescaler is set to divide by eight, the timer will count like this: ... | c-1, c-1, c-1, c-1, c-1, c-1, c-1, c-1 | c, c, c, c, c, c, c, c | 0, 0, 0, 0, 0, 0, 0, 0 | 1, 1, 1, ... in pwm mode, this bit has a different function. if the ctc2 bit is cleared in pwm mode, the timer/counter acts as an up/down counter. if the ctc2 bit is set (one), the timer/counter wraps when it reaches $ff. refer to page 54 for a detailed description.  bits 2, 1, 0 ? cs22, cs21, cs20: clock select bits 2, 1, and 0 the clock select bits 2, 1, and 0 define the prescaling source of timer/counter2. the stop condition provides a timer enable/disable function. the prescaled modes are scaled directly from the pck2 clock. timer/counter2 ? tcnt2 this 8-bit register contains the value of timer/counter2. timer/counters2 is implemented as an up or up/down (in pwm mode) counter with read and write access. if the timer/counter2 is written to and a clock source is selected, it continues counting in the timer clock cycle following the write operation. table 20. timer/counter2 prescale select cs22 cs21 cs20 description 0 0 0 timer/counter2 is stopped. 001pck2 010pck2/8 011pck2/32 100pck2/64 101pck2/128 110pck2/256 1 1 1 pck2/1024 bit 76543210 $24 ($44) msb lsb tcnt2 read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000
54 atmega163(l) 1142e?avr?02/03 timer/counter2 output compare register ? ocr2 the output compare register is an 8-bit read/write register. the timer/counter output compare register contains the data to be continuously com- pared with timer/counter2. actions on compare matches are specified in tccr2. a software write to the timer/counter2 register blocks compare matches in the next timer/counter2 clock cycle. this prevents immediate interrupts when initializing the timer/counter2. a compare match will set the compare interrupt flag in the cpu clock cycle following the compare event. timer/counter2 in pwm mode when pwm mode is selected, the timer/counter2 either wraps (overflows) when it reaches $ff or it acts as an up/down counter. if the up/down mode is selected, the timer/counter2 and the output compare register ? ocr2 form an 8-bit, free-running, glitch-free, and phase correct pwm with outputs on the pd7(oc2) pin. if the overflow mode is selected, the time r/counter2 and the output compare register ? ocr2 form an 8-bit, free-running, and glitch-free pwm, operating with twice the speed of the up/down counting mode. pwm modes (up/down and overflow) the two different pwm modes are selected by the ctc2 bit in the timer/counter con- trol register ? tccr2. if ctc2 is cleared and pwm mode is selected, the timer/counter acts as an up/down counter, counting up from $00 to $ff, where it turns and counts down again to zero before the cycle is repeated. when the counter value matches the contents of the out- put compare register, the pd7(oc2) pin is set or cleared according to the settings of the com21/com20 bits in the timer/counter control register tccr2. if ctc2 is set and pwm mode is selected, the timer/counters will wrap and start count- ing from $00 after reaching $ff. the pd7(oc2) pin will be set or cleared according to the settings of com21/com20 on a timer/counter overflow or when the counter value matches the contents of the output compare register. refer to table 21 for details. bit 76543210 $23 ($43) msb lsb ocr2 read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000
55 atmega163(l) 1142e?avr?02/03 note that in pwm mode, the value to be written to the output compare register is first transferred to a temporary location, and then latched into ocr2 when the timer/counter reaches $ff. this prevents the occurrence of odd-length pwm pulses (glitches) in the event of an unsynchronized ocr2 write. see figure 38 for examples. figure 38. effects of unsynchronized ocr latching table 21. compare mode select in pwm mode ctc2 com21 com20 effect on compare pin frequency 0 0 0 not connected 0 0 1 not connected 01 0 cleared on compare match, up-counting. set on compare match, down-counting (non-inverted pwm). f tck0/2 /510 01 1 cleared on compare match, down-counting. set on compare match, up-counting (inverted pwm). f tck0/2 /510 1 0 0 not connected 1 0 1 not connected 1 1 0 cleared on compare match, set on overflow. f tck0/2 /256 1 1 1 set on compare match, cleared on overflow. f tck0/2 /256 pwm output oc2 pwm output oc2 unsynchronized oc2 latch synchronized oc2 latch
56 atmega163(l) 1142e?avr?02/03 figure 39. effects of unsynchronized ocr latching in overflow mode. during the time between the write and the latch operation, a read from ocr2 will read the contents of the temporary location. this means that the most recently written value always will read out of ocr2. when the output compare register contains $00 or $ff, and the up/down pwm mode is selected, the output pd7(oc2) is updated to low or high on the next compare match according to the settings of com21/com20. this is shown in table 22. in overflow pwm mode, the output pd7(oc2) is held low or high only when the output compare register contains $ff. in up/down pwm mode, the timer overflow flag ? tov2, is set when the counter changes direction at $00. in overflow pwm mode, the timer overflow flag is set as in normal timer/counter mode. the timer overflow interrupt operates exactly as in nor- mal timer/counter mode, i.e., it is executed when tov2 is set provided that timer overflow interrupt and global interrupts are enabled. this also applies to the timer out- put compare flag and interrupt. the frequency of the pwm will be timer clock frequency divided by 510. table 22. pwm outputs ocr2 = $00 or $ff com21 com20 ocr2 output oc2 1 0 $00 l 10$ff h 1 1 $00 h 11$ff l pwm output oc2 pwm output oc2 unsynchronized oc2 latch synchronized oc2 latch counter value compare value counter value compare value compare value changes compare value changes glitch
57 atmega163(l) 1142e?avr?02/03 asynchronous status register ? assr  bit 7..4 ? res: reserved bits these bits are reserved bits in the atmega163 and always read as zero.  bit 3 ? as2: asynchronous timer/counter2 when as2 is cleared (zero), timer/counter2 is clocked from the internal system clock, ck. when as2 is set (one), timer/counter2 is clocked from the pc6(tosc1) pin. pins pc6 and pc7 are connected to a crystal oscillator and cannot be used as general i/o pins. when the value of this bit is changed, the contents of tcnt2, ocr2, and tccr2 might be corrupted.  bit 2 ? tcn2ub: timer/counter2 update busy when timer/counter2 operates asynchronously and tcnt2 is written, this bit becomes set (one). when tcnt2 has been updated from the temporary storage register, this bit is cleared (zero) by hardware. a logical zero in this bit indicates that tcnt2 is ready to be updated with a new value.  bit 1 ? ocr2ub: output compare register2 update busy when timer/counter2 operates asynchronously and ocr2 is written, this bit becomes set (one). when ocr2 has been updated from t he temporary storage register, this bit is cleared (zero) by hardware. a logical zero in this bit indicates that ocr2 is ready to be updated with a new value.  bit 0 ? tcr2ub: timer/counter control register2 update busy when timer/counter2 operates asynchronously and tccr2 is written, this bit becomes set (one). when tccr2 has been updated from the temporary storage register, this bit is cleared (zero) by hardware. a logical zero in this bit indicates that tccr2 is ready to be updated with a new value. if a write is performed to any of the three timer/counter2 registers while its update busy flag is set (one), the updated value might get corrupted and cause an unintentional interrupt to occur. the mechanisms for reading tcnt2, ocr2, and tccr2 are different. when reading tcnt2, the actual timer value is read. when reading ocr2 or tccr2, the value in the temporary storage register is read. bit 7 6 5 4 3 2 1 0 $22 ($22) ? ? ? ? as2 tcn2ub ocr2ub tcr2ub assr read/write r r r r r/w r r r initial value 0 0 0 0 0 0 0 0
58 atmega163(l) 1142e?avr?02/03 asynchronous operation of timer/counter2 when timer/counter2 operates asynchronously, some considerations must be taken.  warning: when switching between asynchronous and synchronous clocking of timer/counter2, the timer registers tcnt2, ocr2, and tccr2 might be corrupted. a safe procedure for switching clock source is: 1. disable the timer/counter2 interrupts by clearing ocie2 and toie2. 2. select clock source by setting as2 as appropriate. 3. write new values to tcnt2, ocr2, and tccr2. 4. to switch to asynchronous operation: wait for tcn2ub, ocr2ub, and tcr2ub. 5. clear the timer/counter2 interrupt flags. 6. enable interrupts, if needed.  the oscillator is optimized for use with a 32.768 khz watch crystal. applying an external clock to the tosc1 pin may result in incorrect timer/counter2 operation. the cpu main clock frequency must be more than four times the oscillator frequency.  when writing to one of the registers tcnt2, ocr2, or tccr2, the value is transferred to a temporary register, and latched after two positive edges on tosc1. the user should not write a new value befor e the contents of the temporary register have been transferred to its destination. each of the three mentioned registers have their individual temporary register, which means that e.g. writing to tcnt2 does not disturb an ocr2 write in progress. to detect that a transfer to the destination register has taken place, the asynchronous status register ? assr has been implemented.  when entering power-save mode after having written to tcnt2, ocr2, or tccr2, the user must wait until the written register has been updated if timer/counter2 is used to wake up the device. otherwise, the mcu will enter sleep mode before the changes are effective. this is particularly important if the output compare2 interrupt is used to wake up the device, since the output compare function is disabled during writing to ocr2 or tcnt2. if the write cycle is not finished, and the mcu enters sleep mode before the ocr2ub bit returns to zero, the device will never receive a compare match interrupt, and the mcu will not wake up.  if timer/counter2 is used to wake the device up from power-save mode, precautions must be taken if the user wants to re-enter power-save mode: the interrupt logic needs one tosc1 cycle to be reset. if the time between wake-up and re-entering power-save mode is less than one tosc1 cycl e, the interrupt will not occur, and the device will fail to wake up. if the user is in doubt whether the time before re-entering power-save is sufficient, the following algorithm can be used to ensure that one tosc1 cycle has elapsed: 1. write a value to tccr2, tcnt2, or ocr2. 2. wait until the corresponding update busy flag in assr returns to zero. 3. enter power-save mode.  when the asynchronous operation is selected, the 32.768 khz oscillator for timer/counter2 is always running, except in power-down mode. after a power-up reset or wake-up from power-down, the user should be aware of the fact that this oscillator might take as long as one second to stabilize. the user is advised to wait for at least one second before using timer/counter2 after power-up or wake-up from power-down. the contents of all timer/counter2 registers must be considered lost after a wake-up from power-down due to unstable clock signal upon startup.  description of wake-up from power-save mode when the timer is clocked asynchronously: when the interrupt condition is met, the wake-up process is started
59 atmega163(l) 1142e?avr?02/03 on the following cycle of the timer clock, that is, the timer is always advanced by at least one before the processor can read the counter value. after wake-up, the mcu is halted for four clock cycles, it executes the interrupt routine, and resumes execution from the instruction following sleep.  during asynchronous operation, the synchronization of the interrupt flags for the asynchronous timer takes three processor cycles plus one timer cycle. the timer is therefore advanced by at least one before the processor can read the timer value causing the setting of the interrupt flag. the output compare pin is changed on the timer clock and is not synchronized to the processor clock.  after waking up from power-save mode with the asynchronous timer enabled, there will be a short interval in which tcnt2 will read as the same value as before power- save mode was entered. after an edge on the asynchronous clock, tcnt2 will read correctly (the compare and overflow functions of the timer are not affected by this behavior.). safe procedure to ensure that the correct value is read: 1. write any value to either of the registers ocr2 or tccr2. 2. wait for the corresponding update busy flag to be cleared. 3. read tcnt2. note that ocr2 and tccr2 are never modified by hardware, and will always read correctly.
60 atmega163(l) 1142e?avr?02/03 watchdog timer the watchdog timer is clocked from a separate on-chip oscillator which runs at 1 mhz. this is the typical value at v cc = 5v. see characterization data for typical values at other v cc levels. by controlling the watchdog timer prescaler, the watchdog reset interval can be adjusted as shown in table 23 on page 61. the wdr ? watchdog reset ? instruction resets the watchdog timer. eight different clock cycle periods can be selected to determine the reset period. if the reset period expires without another watchdog reset, the atmega163 resets and executes from the reset vector. for tim- ing details on the watchdog reset, refer to page 28. to prevent unintentional disabling of the watchdog, a special turn-off sequence must be followed when the watchdog is disabled. refer to the description of the watchdog timer control register for details. figure 40. watchdog timer the watchdog timer control register ? wdtcr  bits 7..5 ? res: reserved bits these bits are reserved bits in th e atmega163 and w ill always read as zero.  bit 4 ? wdtoe: watchdog turn-off enable this bit must be set (one) when the wde bit is cleared. otherwise, the watchdog will not be disabled. once set, hardware will clear this bit to zero after four clock cycles. refer to the description of the wde bit for a watchdog disable procedure.  bit 3 ? wde: watchdog enable when the wde is set (one) the watchdog timer is enabled, and if the wde is cleared (zero) the watchdog timer function is disabled. wde can only be cleared if the wdtoe bit is set(one). to disable an enabled watchdog timer, the following procedure must be followed: 1 mhz at v cc = 5v oscillator bit 765 4 3210 $21 ($41) ? ? ? wdtoe wde wdp2 wdp1 wdp0 wdtcr read/write r r r r/w r/w r/w r/w r/w initial value000 0 0000
61 atmega163(l) 1142e?avr?02/03 1. in the same operation, write a logical one to wdtoe and wde. a logical one must be written to wde even though it is set to one before the disable operation starts. 2. within the next four clock cycles, write a logical 0 to wde. this disables the watchdog.  bits 2..0 ? wdp2, wdp1, wdp0: watchdog timer prescaler 2, 1, and 0 the wdp2, wdp1, and wdp0 bits determine the watchdog timer prescaling when the watchdog timer is enabled. the different prescaling values and their corresponding timeout periods are shown in table 23. table 23. watchdog timer prescale select wdp2 wdp1 wdp0 number of wdt oscillator cycles typical time-out at v cc = 3.0v typical time-out at v cc = 5.0v 0 0 0 16k cycles 47 ms 15 ms 0 0 1 32k cycles 94 ms 30 ms 0 1 0 64k cycles 0.19 s 60 ms 0 1 1 128k cycles 0.38 s 0.12 s 1 0 0 256k cycles 0.75 s 0.24 s 1 0 1 512k cycles 1.5 s 0.49 s 1 1 0 1,024k cycles 3.0 s 0.97 s 1 1 1 2,048k cycles 6.0 s 1.9 s
62 atmega163(l) 1142e?avr?02/03 eeprom read/write access the eeprom access registers are accessible in the i/o space. the write access time is in the range of 1.9 - 3.8 ms, depending on the v cc voltages. see table 24 for details. a self-timing function, however, lets the user software detect when the next byte can be written. if the user code contains code that writes the eeprom, some precautions must be taken. in heavily filtered power supplies, v cc is likely to rise or fall slowly on power-up/down. this causes the device for some period of time to run at a voltage lower than specified as minimum for the clock frequency used. cpu operation under these conditions is likely to cause the program counter to perform unintentional jumps and potentially execute the eeprom write code. to secure eeprom integrity, the user is advised to use an external under-voltage reset circuit or the internal bod in this case. in order to prevent unintentional eeprom writes, a specific write procedure must be fol- lowed. refer to the description of the eeprom control register for details on this. when the eeprom is read, the cpu is halted for four clock cycles before the next instruction is executed. when the eeprom is written, the cpu is halted for two clock cycles before the next instruction is executed. the eeprom addr ess register ? eearh and eearl  bits 15..9 ? res: reserved bits these bits are reserved bits in th e atmega163 and w ill always read as zero.  bits 8..0 ? eear8..0: eeprom address the eeprom address registers ? eearh and eearl ? specify the eeprom address in the 512 bytes eeprom space. the eeprom data bytes are addressed lin- early between 0 and 511. the initial value of eear is undefined. a proper value must be written before the eeprom may be accessed. the eeprom data register ? eedr  bits 7..0 ? eedr7.0: eeprom data for the eeprom write operation, the eedr register contains the data to be written to the eeprom in the address given by the eear register. for the eeprom r ead oper- ation, the eedr contains the data read out from the eeprom at the address given by eear. bit 151413121110 9 8 $1f ($3f) ???????eear8eearh $1e ($3e) eear7 eear6 eear5 eear4 eear3 eear2 eear1 eear0 eearl 76543210 read/write r rrrrrrr/w r/w r/w r/w r/w r/w r/w r/w r/w initial value0000000x xxxxxxxx bit 76543210 $1d ($3d) msb lsb eedr read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000
63 atmega163(l) 1142e?avr?02/03 the eeprom control register ? eecr  bits 7..4 ? res: reserved bits these bits are reserved bits in th e atmega163 and w ill always read as zero.  bit 3 ? eerie: eeprom ready interrupt enable when the i bit in sreg and eerie are set (one), the eeprom ready interrupt is enabled. when cleared (zero), the interrupt is disabled. the eeprom ready interrupt generates a constant interrupt when eewe is cleared (zero).  bit 2 ? eemwe: eeprom master write enable the eemwe bit determines whether setting eewe to one causes the eeprom to be written. when eemwe is set(one) setting eewe will write data to the eeprom at the selected address if eemwe is zero, setting eewe will have no effect. when eemwe has been set (one) by software, hardware clears the bit to zero after four clock cycles. see the description of the eewe bit for an eeprom write procedure.  bit 1 ? eewe: eeprom write enable the eeprom write enable signal eewe is the write strobe to the eeprom. when address and data are correctly set up, the eewe bit must be set to write the value into the eeprom. the eemwe bit must be set when the logical one is written to eewe, otherwise no eeprom write takes place. the following procedure should be followed when writing the eeprom (the order of steps 2 and 3 is not essential): 1. wait until eewe becomes zero. 2. write new eeprom address to eear (optional). 3. write new eeprom data to eedr (opti onal). 4. write a logical one to the eemwe bit in eecr. 5. within four clock cycles after setting eemwe, write a logical one to eewe. caution: an interrupt between step 4 and step 5 will make the write cycle fail, since the eeprom master write enable will time-out. if an interrupt routine accessing the eeprom is interrupting another eeprom access, the eear or eedr register w ill be modified, causing the interrupted eeprom access to fail. it is recommended to have the global interrupt flag cleared during the four last steps to avoid these problems. when the write access time (see table 24) has elapsed, the eewe bit is cleared (zero) by hardware. the user software can poll this bit and wait for a zero before writing the next byte. when eewe has been set, the cpu is halted for four cycles before the next instruction is executed.  bit 0 ? eere: eeprom read enable the eeprom read enable signal eere is the read strobe to the eeprom. when the correct address is set up in the eear register, the eere bit must be set. when the eere bit is cleared (zero) by hardware, requested data is found in the eedr register. the eeprom read access takes one instruction, and there is no need to poll the eere bit. when eere has been set, the cpu is halt ed for two cycles before the next instruc- tion is executed. bit 76543 2 10 $1c ($3c) ????eerieeemweeeweeereeecr read/write r r r r r/w r/w r/w r/w initial value00000 0 00
64 atmega163(l) 1142e?avr?02/03 the user should poll the eewe bit before starting the read operation. if a write operation is in progress, it is not possible to set the eere bit, nor to change the eear register. the calibrated oscillator is used to time the eeprom accesses. table 24 lists the typi- cal programming time for eeprom access from the cpu preventing eeprom corruption during periods of low v cc, the eeprom data can be corrupted because the supply volt- age is too low for the cpu and the eeprom to operate properly. these issues are the same as for board level systems using the eeprom, and the same design solutions should be applied. an eeprom data corruption can be caused by two situations when the voltage is too low. first, a regular write sequence to the eeprom requires a minimum voltage to operate correctly. secondly, the cpu itself can execute instructions incorrectly, if the supply voltage for executing instructions is too low. eeprom data corruption can easily be avoided by following these design recommen- dations (one is sufficient): 1. keep the avr r eset active (low) during periods of insufficient power s upply voltage. this can be done by enabling the internal brown-out detector (bod) if the operating voltage matches the detection level. if not, an external low v cc reset protection circuit can be used. if a reset occurs while a write operation is in progress, the write operation will be completed provided that the power supply is voltage is sufficient. 2. keep the avr core in power-down sleep mode during periods of low v cc . this will prevent the cpu from attempting to decode and execute instructions, effec- tively protecting the eeprom registers from unintentional writes. 3. store constants in flash memory if the ability to change memory contents from software is not required. flash memory can not be updated by the cpu unless the boot loader software supports writing to the flash and the boot lock bits are configured so that writing to the flash memory from cpu is allowed. see ?boot loader support? on page 134 for details. table 24. eeprom programming time. symbol number of calibrated rc oscillator cycles min programmingn time max programming time eeprom write (from cpu) 2048 1.9 ms 3.8 ms
65 atmega163(l) 1142e?avr?02/03 serial peripheral interface ? spi the serial peripheral interface (spi) allows high-speed synchronous data transfer between the atmega163 and peripheral devices or between several avr devices. the atmega163 spi includes the following features:  full-duplex, three-wire synchronous data transfer  master or slave operation  lsb first or msb first data transfer  seven programmable bit rates  end of transmission interrupt flag  write collision flag protection  wake-up from idle mode  double speed (ck/2) master spi mode figure 41. spi block diagram the interconnection between master and slave cpus with spi is shown in figure 42. the pb7(sck) pin is the clock output in the master mode and the clock input in the slave mode. writing to the spi data register of the master cpu starts the spi clock generator, and the data written shifts out of the pb5(mosi) pin and into the pb5(mosi) pin of the slave cpu. after shifting one byte, the spi clock generator stops, setting the end of transmission flag (spif). if the spi interrupt enable bit (spie) in the spcr register is set, an interrupt is requested. the slave select input, pb4(ss ), is set low to select an individual slave spi device. the two shift registers in the master and the slave can be considered as one distributed 16-bit circular shift register. this is shown in figure 42. when data is shifted from the master to the slave, data is also shifted in spi2x spi2x divider /2/4/8/16/32/64/128
66 atmega163(l) 1142e?avr?02/03 the opposite direction, simultaneously. during one shift cycle, data in the master and the slave is interchanged. figure 42. spi master-slave interconnection the system is single buffered in the transmit direction and double buffered in the receive direction. this means that bytes to be transmitted cannot be written to the spi data register before the entire shift cycle is completed. when receiving data, however, a received character must be read from the spi data register before the next character has been completely shifted in. otherwise, the first byte is lost. when the spi is enabled, the data direction of the mosi, miso, sck, and ss pins is overridden according to table 25. note: 1. see ?alternate functions of portb? on page 118 for a detailed description of how to define the direction of the user defined spi pins. ss pin functionality when the spi is configured as a master (mstr in spcr is set), the user can determine the direction of the ss pin. if ss is configured as an output, the pin is a general output pin which does not affect the spi system. if ss is configured as an input, it must be held high to ensure master spi operation. if the ss pin is driven low by peripheral circuitry when the spi is configured as a master with the ss pin defined as an input, the spi sys- tem interprets this as another master selecting the spi as a slave and starting to send data to it. to avoid bus contention, the spi system takes the following actions: 1. the mstr bit in spcr is cleared and the spi system becomes a slave. as a result of the spi becoming a slave, the mosi and sck pins become inputs. 2. the spif flag in spsr is set, and if the spi interrupt is enabled, and the i-bit in sreg is set, the interrupt routine will be executed. thus, when interrupt-driven spi transmission is used in master mode, and there exists a possibility that ss is driven low, the interrupt should always check that the mstr bit is still set. if the mstr bit has been cleared by a slave select, it must be set by the user to re-enable spi master mode. table 25. spi pin overrides (1) pin direction, master spi direction, slave spi mosi user defined input miso input user defined sck user defined input ss user defined input
67 atmega163(l) 1142e?avr?02/03 when the spi is configured as a slave, the ss pin is always input. when ss is held low, the spi is activated, and miso becomes an output if configured so by the user. all other pins are inputs. when ss is driven high, all pins are inputs, and the spi is passive, which means that it will not receive incoming data. note that the spi logic will be reset once the ss pin is driven high. if the ss pin is driven high during a transmission, the spi will stop sending and receiving immediately and both data received and data sent must be considered as lost. data modes there are four combinations of sck phase and polarity with respect to serial data, which are determined by control bits cpha and cpol. the spi data transfer formats are shown in figure 43 and figure 44. figure 43. spi transfer format with cpha = 0 and dord = 0 figure 44. spi transfer format with cpha = 1 and dord = 0 spi control register ? spcr  bit 7 ? spie: spi interrupt enable this bit causes the spi interrupt to be executed if spif bit in the spsr register is set and the if the global interrupt enable bit in sreg is set.  bit 6 ? spe: spi enable when the spe bit is set (one), the spi is enabled. this bit must be set to enable any spi operations. bit 76543210 $0d ($2d) spie spe dord mstr cpol cpha spr1 spr0 spcr read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000
68 atmega163(l) 1142e?avr?02/03  bit 5 ? dord: data order when the dord bit is set (one), the lsb of the data word is transmitted first. when the dord bit is cleared (zero), the msb of the data word is transmitted first.  bit 4 ? mstr: master/slave select this bit selects master spi mode when set (one), and slave spi mode when cleared (zero). if ss is configured as an input and is driven low while mstr is set, mstr will be cleared, and spif in spsr will become set. the user will then have to set mstr to re- enable spi master mode.  bit 3 ? cpol: clock polarity when this bit is set (one), sck is high when idle. when cpol is cleared (zero), sck is low when idle. refer to figure 43 and figure 44 for additional information.  bit 2 ? cpha: clock phase refer to figure 43 and figure 44 for the functionality of this bit.  bits 1, 0 ? spr1, spr0: spi clock rate select 1 and 0 these two bits control the sck rate of the device configured as a master. spr1 and spr0 have no effect on the slave. the relationship between sck and the oscillator clock frequency f ck is shown in the following table: the spi status register ? spsr  bit 7 ? spif : spi interrupt flag when a serial transfer is complete, the spif bit is set (one) and an interrupt is gener- ated if spie in spcr is set (one) and global interrupts are enabled. if ss is an input and is driven low when the spi is in master mode, this will also set the spif flag. spif is cleared by hardware when executing the corresponding interrupt handling vector. alter- natively, the spif bit is cleared by first reading the spi status register with spif set (one), then accessing the spi data register (spdr). table 26. relationship between sck and the oscillator frequency spi2x spr1 spr0 sck frequency 000 f ck / 4 001 f ck / 16 010 f ck / 64 011 f ck / 128 100 f ck / 2 101 f ck / 8 110 f ck / 32 111 f ck / 64 bit 76543210 $0e ($2e) spifwcol?????spi2xspsr read/write r rrrrrrr/w initial value00000000
69 atmega163(l) 1142e?avr?02/03  bit 6 ? wcol : write collision flag the wcol bit is set if the spi data register (spdr) is written during a data transfer. the wcol bit (and the spif bit) are cleared (z ero) by first reading the spi status reg- ister with wcol set (one), and then accessing the spi data register.  bit 5..1 ? res: reserved bits these bits are reserved bits in th e atmega163 and w ill always read as zero.  bit 0 ? spi2x: double spi speed bit when this bit is set (one) the spi speed (sck frequency) will be doubled when the spi is in master mode (see table 26). this means that the minimum sck period will be two cpu clock periods. when the spi is configured as slave, the spi is only guaranteed to work at f ck /4 or lower. the spi interface on the atmega163 is also used for program memory and eeprom downloading or uploading. see page 155 for serial programming and verification. the spi data register ? spdr the spi data register is a read/write register used for data transfer between the regis- ter file and the spi shift register. writing to the register initiates data transmission. reading the register causes the shift register receive buffer to be read. bit 76543210 $0f ($2f) msb lsb spdr read/write r/w r/w r/w r/w r/w r/w r/w r/w initial valuexxxxxxxxundefined
70 atmega163(l) 1142e?avr?02/03 uart the atmega163 features a full duplex (separate receive and transmit registers) uni- versal asynchronous receiver and transmitter (uart). the main features are:  baud rate generator generates any baud rate  high baud rates at low xtal frequencies  8 or 9 bits data  noise filtering  overrun detection  framing error detection  false start bit detection  three separate interrupts on tx complete, tx data register empty, and rx complete  multi-processor communication mode  double speed uart mode data transmission a block schematic of the uart transmitter is shown in figure 45. figure 45. uart transmitter data transmission is initiated by writing the data to be transmitted to the uart i/o data register, udr. data is transferred from udr to the transmit shift register when:  a new character has been written to udr after the stop bit from the previous character has been shifted out. the shift register is loaded immediately.  a new character has been written to udr be fore the stop bit from the previous character has been shifted out. the shift register is loaded when the stop bit of the character currently being transmitted has been shifted out.
71 atmega163(l) 1142e?avr?02/03 when data is transferred from udr to the shift register, the udre (uart data regis- ter empty) bit in the uart status register, usr, is set. when this bit is set (one), the uart is ready to receive the next character. at the same time as the data is transferred from udr to the 10(11)-bit shift register, bit 0 of the shift register is cleared (start bit) and bit 9 or 10 is set (stop bit). if 9-bit data word is selected (the chr9 bit in the uart control register, ucr is set), the txb8 bit in ucr is transferred to bit 9 in the transmit shift register. on the baud rate clock following the transfer operation to the shift register, the start bit is shifted out on the txd pin. then follows the data, lsb first. when the stop bit has been shifted out, the shift register is loaded if any new data has been written to the udr during the transmission. during loading, udre is set. if there is no new data in the udr register to send when the stop bit is shifted out, the udre flag will remain set until udr is written again. when no new data has been written, and the stop bit has been present on txd for one bit length, the transmit complete flag, txc, in usr is set. the txen bit in ucr enables the uart transmitter when set (one). when this bit is cleared (zero), the pd1 pin can be used for general i/o. when txen is set, the uart transmitter will be connected to pd1, which is forced to be an output pin regardless of the setting of the ddd1 bit in ddrd.
72 atmega163(l) 1142e?avr?02/03 data reception figure 46 shows a block diagram of the uart receiver figure 46. uart receiver the receiver front-end logic samples the signal on the rxd pin at a frequency 16 times the baud rate. while the line is idle, one single sample of logical zero will be interpreted as the falling edge of a start bit, and the start bit detection sequence is initiated. let sam- ple 1 denote the first zero-sample. following the 1 to 0-transition, the receiver samples the rxd pin at samples 8, 9, and 10. if two or more of these three samples are found to be logical ones, the start bit is rejected as a noise spike and the receiver starts looking for the next 1 to 0-transition. if however, a valid start bit is detected, sampling of the data bits following the start bit is performed. these bits are also sampled at samples 8, 9, and 10. the logical value found in at least two of the three samples is taken as the bit value. all bits are shifted into the transmitter shift register as they are sampled. sampling of an incoming character is shown in figure 47. note that the description above is not valid when the uart trans- mission speed is doubled. see ?double speed transmission? on page 78 for a detailed description.
73 atmega163(l) 1142e?avr?02/03 figure 47. sampling received data (1) note: 1. this figure is not valid when the uart speed is doubled. see ?double speed trans- mission? on page 78 for a detailed description. when the stop bit enters the receiver, the majority of the three samples must be one to accept the stop bit. if two or more samples are logical zeros, the framing error (fe) flag in the uart status register (usr) is set. before reading the udr register, the user should always check the fe bit to detect framing errors. whether or not a valid stop bit is detected at the end of a character reception cycle, the data is transferred to udr and the rxc flag in usr is set. udr is in fact two physically separate registers, one for transmitted data and one for received data. when udr is read, the receive data register is accessed, and when udr is written, the transmit data register is accessed. if 9-bit data wo rd is selected (the chr9 bit in the uart con- trol register, ucr is set), the rxb8 bit in ucr is loaded with bit nine in the transmit shift register when data is transferred to udr. if, after having received a character, the udr register has not been read since the last receive, the overrun (or) flag in ucr is set. this means that the la st data byte shifted into to the shift register could not be transferred to udr and has been lost. the or bit is buffered, and is updated when the valid data byte in udr is read. thus, the user should always check the or bit when reading the udr register in order to detect any overruns if the baud rate is high or cpu load is high. when the rxen bit in the ucr register is cl eared (zero), the receiver is disabled. this means that the pd0 pin can be used as a general i/o pin. when rxen is set, the uart receiver will be connected to pd0, which is forced to be an input pin regardless of the setting of the ddd0 bit in ddrd. when pd0 is forced to input by the uart, the portd0 bit can still be used to control the pull-up resistor on the pin. when the chr9 bit in the ucr register is se t, transmitted and rece ived characters are 9-bit long plus start and stop bits. the ninth data bit to be transmitted is the txb8 bit in ucr register. this bit must be set to the wanted value before a transmission is initated by writing to the udr register. the 9th data bit received is the rxb8 bit in the ucr register. it is important that the status register (usr) always is read before the data register (udr). the data register should be read only once for each received byte. otherwise, the status register (usr) might get updated with incorrect values. multi-processor communication mode the multi-processor communication mode enables several slave mcus to receive data from a master mcu. this is done by first decoding an address byte to find out which mcu has been addressed. if a particular sl ave mcu has been address ed, it will receive the following data bytes as normal, while the other slave mcus will ignor e the data bytes until another address byte is received. for an mcu to act as a master mcu, it should enter 9-bit transmission mode (chr9 in ucsrb set). the ninth bit must be one to indicate that an address byte is being trans- mitted, and zero to indicate that a data byte is being transmitted. for the slave mcus, the mechanism appears slightly differently for 8-bit and 9-bit reception mode. in 8-bit reception mode (chr9 in ucsrb cleared), the stop bit is one for an address byte and zero for a data by te. in 9-bit reception mode (chr9 in ucsrb
74 atmega163(l) 1142e?avr?02/03 set), the 9th bit is one for an address byte and zero for a data byte, whereas the stop bit is always high. the following procedure should be used to exchange data in multi-processor communi- cation mode: 1. all slave mcus are in multi-processor communication mode (mpcm in ucsra is set). 2. the master mcu sends an address byte, and all slaves receive and read this byte. in the slave mcus, the rxc flag in ucsra will be set as normal. 3. each slave mcu reads the udr register and determines if it has been selected. if so, it clears the mpcm bit in ucsra, otherwise it waits for the next address byte. 4. for each received data byte, the receiving mcu will set the receive complete flag (rxc in ucsra). in 8-bit mode, the receiving mcu will also generate a framing error (fe in ucsra set), since the stop bit is zero. the other slave mcus, which still have the mpcm bit set, will ignore the data byte. in this case, the udr register and the rxc or fe flags will not be af fected. 5. after the last byte has been transferred, the process repeats from step 2. uart control uart i/o data register ? udr the udr register is actually two physically separate registers sharing the same i/o address. when writing to the register, the uart transmit data register is written. when reading from udr, the uart receive data register is read. uart control and status register a ? ucsra  bit 7 ? rxc: uart receive complete this bit is set (one) when a received character is transferred from the receiver shift register to udr. the bit is set regardles s of any detected framing errors. when the rxcie bit in ucr is set, the uart receive complete interrupt will be executed when rxc is set(one). rxc is cleared by reading udr. when interrupt-driven data reception is used, the uart receive complete interr upt routine must read udr in order to clear rxc, otherwise a new interrupt will occur once the interrupt routine terminates.  bit 6 ? txc: uart transmit complete this bit is set (one) when the entire character (including the stop bit) in the transmit shift register has been shifted out and no new data has been written to udr. this flag is especially useful in half-duplex communications interfaces, where a transmitting appli- cation must enter receive mode and free the communications bus immediately after completing the transmission. bit 76543210 $0c ($2c) msb lsb udr read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000 bit 76543210 $0b ($2b) rxc txc udre fe or ? u2x mpcm ucsra read/write r r/w rrrrr/wr/w initial value00000000
75 atmega163(l) 1142e?avr?02/03 when the txcie bit in ucr is set, setting of txc causes the uart transmit complete interrupt to be executed. txc is cleared by hardware when executing the corresponding interrupt handling vector. alternatively, the txc bit is cleared (zero) by writing a logical one to the bit.  bit 5 ? udre: uart data register empty this bit is set (one) when a character writte n to udr is transferred to the transmit shift register. setting of this bit indicates that the transmitter is ready to receive a new char- acter for transmission. when the udrie bit in ucr is set, the uart transmit complete interrupt to be exe- cuted as long as udre is set. udre is cleared by writing udr. when interrupt-driven data transmittal is used, the uart data register empty interrupt routine must write udr in order to clear udre, otherwise a new interrupt w ill occur once the interrupt rou- tine terminates. udre is set (one) during reset to indicate that the transmitter is ready.  bit 4 ? fe: framing error this bit is set if a framing error condition is detected, i.e. when the stop bit of an incom- ing character is zero. the fe bit is cleared when the stop bit of received data is one.  bit 3 ? or: overrun this bit is set if an overrun condition is detected, i.e., when a character already present in the udr register is not read before the next character has been shifted into the receiver shift register. the or bit is buffered, which means that it will be set once the valid data still in udr is read. the or bit is cleared (zero) when data is received and transferred to udr.  bit 2 ? res: reserved bit this bit is a reserved bit in the at mega163 and w ill always read as zero.  bits 1 ? u2x: double the uart transmission speed setting this bit will reduce th e division of the baud rate generator clock from 16 to 8, effectively doubling the transfer speed at the expense of robustness. for a detailed description, see ?double speed transmission? on page 78.  bit 0 ? mpcm: multi-processor communication mode this bit is used to enter multi-processor communication mode. the bit is set when the slave mcu waits for an address byte to be received. when the mcu has been addressed, the mcu switches off the mpcm bit, and starts data reception. for a detailed description, see ?multi-processor communication mode? on page 73.
76 atmega163(l) 1142e?avr?02/03 uart control and status register b ? ucsrb  bit 7 ? rxcie: rx complete interrupt enable when this bit is set (one), a setting of t he rxc bit in usr will cause the receive com- plete interrupt routine to be executed provided that global interrupts are enabled.  bit 6 ? txcie: tx complete interrupt enable when this bit is set (one), a setting of th e txc bit in usr will cause the transmit com- plete interrupt routine to be executed provided that global interrupts are enabled.  bit 5 ? udrie: uart data register empty interrupt enable when this bit is set (one), a setting of the udre bit in usr will cause the uart data register empty interrupt routine to be executed provided that global interrupts are enabled.  bit 4 ? rxen: receiver enable this bit enables the uart receiver when set (one). when the receiver is disabled, the rxc, or, and fe status flags cannot become set. if these flags are set, turning off rxen does not cause them to be cleared.  bit 3 ? txen: transmitter enable this bit enables the uart transmitter when set (one). when disabling the transmitter while transmitting a character, the transmitter is not disabled before the character in the shift register plus any following char acter in udr has been completely transmitted.  bit 2 ? chr9: 9-bit characters when this bit is set (one) transmitted and received characters are 9-bit long plus start and stop bits. the ninth bit is read and writ ten by using t he rxb8 and txb8 bits in ucr, respectively. the ninth data bit can be used as an extra stop bit or a parity bit.  bit 1 ? rxb8: receive data bit 8 when chr9 is set (one), rxb8 is the ninth data bit of the received character.  bit 0 ? txb8: transmit data bit 8 when chr9 is set (one), txb8 is the ninth data bit in the character to be transmitted. baud rate generator the baud rate generator is a frequency divider which generates baud-rates according to the following equation:  baud = baud rate f ck = crystal clock frequency  ubr = contents of the ubrrhi and ubrr registers, (0 - 4095) bit 76543210 $0a ($2a) rxcie txcie udrie rxen txen chr9 rxb8 txb8 ucsrb read/write r/w r/w r/w r/w r/w r/w r w initial value00000010 baud f ck 16(ubr 1 ) + --------------------------------- =
77 atmega163(l) 1142e?avr?02/03  note that this equation is not valid when the uart transmission speed is doubled. see ?double speed transmission? on page 78 for a detailed description. for standard crystal frequencies, the most commonly used baud rates can be generated by using the ubr settings in table 27. ubr values which yield an actual baud rate dif- fering less than 2% from the target baud rate, are bold in the table. however, using baud rates that have more than 1% error is not recommended. high error ratings give less noise resistance. table 27. ubr settings at various crystal frequencies baud rate 1mhz %error 1,84 mhz %error 2mhz %error 2,458 mhz %error 2400 ubr= 25 0,2 ubr= 47 0,0 ubr= 51 0,2 ubr= 63 0,0 4800 ubr= 12 0,2 ubr= 23 0,0 ubr= 25 0,2 ubr= 31 0,0 9600 ubr= 6 7,5 ubr= 11 0,0 ubr= 12 0,2 ubr= 15 0,0 14400 ubr= 3 7,8 ubr= 70,0 ubr= 8 3,7 ubr= 10 3,1 19200 ubr= 2 7,8 ubr= 50,0 ubr= 6 7,5 ubr= 70,0 28800 ubr= 1 7,8 ubr= 30,0 ubr= 3 7,8 ubr= 4 6,3 38400 ubr= 1 22,9 ubr= 20,0 ubr= 2 7,8 ubr= 30,0 57600 ubr= 0 7,8 ubr= 10,0 ubr= 1 7,8 ubr= 2 12,5 76800 ubr= 0 22,9 ubr= 1 33,3 ubr= 1 22,9 ubr= 10,0 115200 ubr= 0 84,3 ubr= 00,0 ubr= 0 7,8 ubr= 0 25,0 baud rate 3,28 mhz %error 3,69 mhz %error 4mhz %error 4,608 mhz %error 2400 ubr= 84 0,4 ubr= 95 0,0 ubr= 103 0,2 ubr= 119 0,0 4800 ubr= 42 0,8 ubr= 47 0,0 ubr= 51 0,2 ubr= 59 0,0 9600 ubr= 20 1,6 ubr= 23 0,0 ubr= 25 0,2 ubr= 29 0,0 14400 ubr= 13 1,6 ubr= 15 0,0 ubr= 16 2,1 ubr= 19 0,0 19200 ubr= 10 3,1 ubr= 11 0,0 ubr= 12 0,2 ubr= 14 0,0 28800 ubr= 61,6 ubr= 70,0 ubr= 8 3,7 ubr= 90,0 38400 ubr= 4 6,3 ubr= 50,0 ubr= 6 7,5 ubr= 7 6,7 57600 ubr= 3 12,5 ubr= 30,0 ubr= 3 7,8 ubr= 40,0 76800 ubr= 2 12,5 ubr= 20,0 ubr= 2 7,8 ubr= 3 6,7 115200 ubr= 1 12,5 ubr= 10,0 ubr= 1 7,8 ubr= 2 20,0 baud rate 7,37 mhz %error 8mhz %error 2400 ubr= 191 0,0 ubr= 207 0,2 4800 ubr= 95 0,0 ubr= 103 0,2 9600 ubr= 47 0,0 ubr= 51 0,2 14400 ubr= 31 0,0 ubr= 34 0,8 19200 ubr= 23 0,0 ubr= 25 0,2 28800 ubr= 15 0,0 ubr= 16 2,1 38400 ubr= 11 0,0 ubr= 12 0,2 57600 ubr= 70,0 ubr= 8 3,7 76800 ubr= 50,0 ubr= 6 7,5 115200 ubr= 30,0 ubr= 3 7,8
78 atmega163(l) 1142e?avr?02/03 uart baud rate registers ? ubrr and ubrrhi this is a 12-bit register which contains the uart baud rate according to the equation on the previous page. the ubrrhi contains the four most significant bits, and the ubrr contains the eight least significant bits of the uart baud rate. double speed transmission the atmega163 provides a separate uart mode which allows the user to double the communication speed. by setting the u2x bit in the uart control and status register ucsra, the uart speed will be doubled. note, however, that the receiver will in this case only use half the number of samples (only 8 instead of 16) for data sampling and clock recovery, and therefore requires more accurate baud ra te setting and system clock. the data reception will differ slightly from normal mode. since the speed is doubled, the receiver front-end logic samples the signals on rxd pin at a frequency eight times the baud rate. while the line is idle, one single sample of logical zero will be interpreted as the falling edge of a start bit, and the start bit detection sequence is initiated. let sample 1 denote the first zero-sample. following the 1 to 0-transition, the receiver samples the rxd pin at samples 4, 5, and 6. if two or more of these three samples are found to be logical ones, the start bit is rejected as a noise spike and the receiver starts looking for the next 1 to 0-transition. if however, a valid start bit is detected, sampling of the data bits following the start bit is performed. these bits are also sampled at samples 4, 5, and 6. the logical value found in at least two of the three samples is taken as the bit value. all bits are shifted into the transmitter shift register as they are sampled. sampling of an incoming character is shown in figure 48. figure 48. sampling received data when the transmission speed is doubled the baud rate generator in double uart speed mode note that the baud-rate equation is different from the equation on page 78 when the uart speed is doubled:  baud = baud rate f ck = crystal clock frequency  ubr = contents of the ubrrhi and ubrr registers, (0 - 4095)  note that this equation is only valid when the uart transmission speed is doubled. for standard crystal frequencies, the most commonly used baud rates can be generated by using the ubr settings in table 28. ubr values which yield an actual baud rate dif- fering less than 1.5% from the target baud rate, are bold in the table. however, since the bit 151413121110 9 8 $20 ($40) ????msb lsb ubrrhi $09 ($29) msb lsb ubrr 76543210 read/write r r r r r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000 00000000 start bit d0 d1 d2 d3 d4 d5 d6 d7 stop bit rxd receiver sampling baud f ck 8(ubr 1 ) + ------------------------------ =
79 atmega163(l) 1142e?avr?02/03 number of samples are reduced, and the system clock might have some variance (this applies especially when using resonators), it is recommended that the baud rate error is less than 0.5%. table 28. ubr settings at various crystal frequencies in double speed mode 1.0000 mhz % error 1.8432 mhz % error 2.0000 mhz % error ubr = 51 0.2 ubr = 95 0.0 ubr = 103 0.2 ubr = 25 0.2 ubr = 47 0.0 ubr = 51 0.2 ubr = 12 0.2 ubr = 23 0.0 ubr = 25 0.2 ubr = 8 3.7 ubr = 15 0.0 ubr = 16 2.1 ubr = 6 7.5 ubr = 11 0.0 ubr = 12 0.2 ubr = 3 7.8 ubr = 70.0 ubr = 8 3 . 7 ubr = 2 7.8 ubr = 50.0 ubr = 6 7 . 5 ubr = 1 7.8 ubr = 30.0 ubr = 3 7 . 8 ubr = 1 22.9 ubr = 20.0 ubr = 2 7 . 8 ubr = 0 84.3 ubr = 1 0.0 ubr = 1 7 . 8 -- ubr = 0 0.0 -- 3.2768 mhz % error 3.6864 mhz % error 4.0000 mhz % error ubr = 170 0.2 ubr = 191 0.0 ubr = 207 0.2 ubr = 84 0.4 ubr = 95 0.0 ubr = 103 0.2 ubr = 42 0.8 ubr = 47 0.0 ubr = 51 0.2 ubr = 27 1.6 ubr = 31 0.0 ubr = 34 0.8 ubr = 20 1.6 ubr = 23 0.0 ubr = 25 0.2 ubr = 13 1.6 ubr = 15 0.0 ubr = 16 2.1 ubr = 10 3.1 ubr = 11 0.0 ubr = 12 0.2 ubr = 6 1.6 ubr = 70.0 ubr = 8 3 . 7 ubr = 4 6.2 ubr = 50.0 ubr = 6 7 . 5 ubr = 3 12.5 ubr = 30.0 ubr = 3 7 . 8 ubr = 1 12.5 ubr = 10.0 ubr = 1 7 . 8 ubr = 0 12.5 ubr = 0 0.0 ubr = 0 7 . 8 7.3728 mhz % error 8.0000 mhz % error ubr = 383 0.0 ubr = 416 0.1 ubr = 191 0.0 ubr = 207 0.2 ubr = 95 0.0 ubr = 103 0.2 ubr = 63 0.0 ubr = 68 0.6 ubr = 47 0.0 ubr = 51 0.2 ubr = 31 0.0 ubr = 34 0.8 ubr = 23 0.0 ubr = 25 0.2 ubr = 15 0.0 ubr = 16 2.1 ubr = 11 0.0 ubr = 12 0.2 ubr = 7 0.0 ubr = 8 3.7 ubr = 30.0 ubr = 3 7.8 ubr = 1 0.0 ubr = 1 7.8 ubr = 0 0.0 ubr = 0 7.8
80 atmega163(l) 1142e?avr?02/03 two-wire serial interface (byte oriented) the two-wire serial interface supports bi-directional serial communication. it is designed primarily for simple but efficient integrated circuit (ic) control. the system is comprised of two lines, scl (serial clock) and sda (serial data) that carry information between the ics connected to them. various communication configurations can be designed using this bus. figure 49 shows a typical two-wire serial bus configuration. any device connected to the bus can be master or slave. note that all avr devices con- nected to the bus must be powered to allow any bus operation. figure 49. two-wire serial bus configuration the two-wire serial interface supports master/slave and transmitter/receiver opera- tion at up to 217 khz bus clock rate. the two-wire serial interface has hardware support for 7-bit addressing, but is easily extended to, e.g., a 10-bit addressing format in software. when the two-wire serial interface is enabled (twen in twcr is set), a glitch filter is enabled for the input signals from the pins pc0 (scl) and pc1 (sda), and the output from these pins is slew-rate controlled. the two-wire serial interface is byte oriented. the operation of the two-wire serial bus is shown as a pulse diagram in fig- ure 50, including the start and stop conditions and generation of ack signal by the bus receiver. figure 50. two-wire serial bus timing diagram the block diagram of the two-wire serial interface is shown in figure 51. device 1 device 2 device 3 device n ....... v cc r1 r2 scl sda sda scl msb r/w bit stop condition start condition repeated start condition 12 789 12 89 ack ack acknowledge from receiver
81 atmega163(l) 1142e?avr?02/03 figure 51. block diagram of the two-wire serial interface the cpu interfaces with the two-wire serial interface via the following five i/o regis- ters: the two-wire serial interface bit rate register (twbr), the two-wire serial interface control register (twcr), the two-wire serial interface status register (twsr), the two-wire serial interface data register (twdr), and the two-wire serial interface address register (twar, used in slave mode). ack input output input output start/stop and sync arbitration timing and control serial clock generator state machine and status decoder data shift register address register and comparator twar sda scl twdr control register status register status twcr twsr avr 8-bit data bus
82 atmega163(l) 1142e?avr?02/03 the two-wire serial interface bit rate register ? twbr  bits 7..0 ? two-wire serial interface bit rate register twbr selects the division factor for the bit rate generator. the bit rate generator is a frequency divider which generates the scl clock frequency in the master modes according to the following equation:  bit rate = scl frequency f ck = cpu clock frequency  twbr = contents of the two-wire serial interface bit rate register t a = bus alignment adjustion note: both the receiver and the transmitter can stretch the low period of the scl line when waiting for user response, thereby reducing the average bit rate. twbr should be set to a value higher than seven to ensure correct two-wire serial bus functionality. the bus alignment adjustion is automatically inserted by the two-wire serial interface, and ensures the validity of setup and hold times on the bus for any twbr value higher than seven. this adjustment may vary from 200 ns to 600 ns depending on bus loads and drive capabilities of the devices connected to the bus. the two-wire serial interface control register ? twcr  bit 7 ? twint: two-wire serial interface interrupt flag this bit is set by hardware when the two-wire serial interface has finished its current job and expects application software response. if the i-bit in the sreg and twie in the twcr register are set (one), the mcu will jump to the interrupt vector at address $22. while the twint flag is set, the bus scl clock line low period is stretched. the twint flag must be cleared by software by writing a logic one to it. note that this flag is not automaticaly cleared by hardware when executing the interrupt routine. also note that clearing this flag starts the operation of the two-wire serial interface, so all accesses to the two-wire serial interface address register ? twar, two-wire serial interface sta- tus register ? twsr, and two-wire serial interface data register ? twdr must be complete before clearing this flag. bit 76543210 $00 ($20) twbr7 twbr6 twbr5 twbr4 twbr3 twbr2 twbr1 twbr0 twbr read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000 bit rate f ck 16 2(twbr) + t a f ck + ----------------------------------------------------------- = bit 76543210 $36 ($56) twint twea twsta twsto twwc twen ? twie twcr read/write r/w r/w r/w r/w r r/w r r/w initial value00000000
83 atmega163(l) 1142e?avr?02/03  bit 6 ? twea: two-wire serial interface enable acknowledge flag twea flag controls the generation of the acknowledge pulse. if the twea bit is set, the ack pulse is generated on the two-wire serial bus if the following conditions are met: 1. the device?s own slave address has been received. 2. a general call has been received, while the twgce bit in the twar is set. 3. a data byte has been received in master receiver or slave receiver mode. by setting the twea bit low, the device can be virtually disconnected from the two-wire serial bus temporarily. address recognition can then be resumed by setting the twea bit again.  bit 5 ? twsta: two-wire serial bus start condition flag the twsta flag is set by the application when it desires to become a master on the two-wire serial bus. the two-wire serial interface hardware checks if the bus is avail- able, and generates a start condition on the bus if it is free. however, if the bus is not free, the two-wire serial interface waits until a stop condition is detected, and then generates a new start condition to claim the bus master status.  bit 4 ? twsto: two-wire serial bus stop condition flag twsto is a stop condition flag. in master mode setting the twsto bit in the control register will generate a stop condition on the two-wire serial bus. when the stop condition is executed on the bus, the twsto bit is cleared automatically. in slave mode setting the twsto bit can be used to recover from an error condition. no stop condition is generated on the bus then, but the two-wire serial interface returns to a well-defined unaddressed slave mode and releases the scl and sda lines to a high impedance state.  bit 3 ? twwc: two-wire serial bus write collision flag the twwc bit is set when attempting to write to the two-wire serial interface data register ? twdr when twint is low. this flag is cleared by writing the twdr register when twint is high.  bit 2 ? twen: two-wire serial interface enable bit the twen bit enables two-wire serial interface operation. if this bit is cleared (zero), the bus outputs sda and scl are set to high impedance state, and the input signals are ignored. the interface is activated by setting this bit (one).  bit 1 ? res: reserved bit this bit is a reserved bit in the at mega163 and w ill always read as zero.
84 atmega163(l) 1142e?avr?02/03  bit 0 ? twie: two-wire serial interface interrupt enable when this bit is enabled, and the i-bit in sreg is set, the two-wire serial interface inter- rupt will be activated for as long as the twint flag is high. the twcr is used to control the operation of the two-wire serial interface. it is used to enable the two-wire serial interface, to initiate a master access by applying a start condition to the bus, to generate a receiver acknowledge, to generate a stop condition, and to control halting of the bus while the data to be written to the bus are written to the twdr. it also indicates a write collision if data is attempted written to twdr while the register is inaccessible. the two-wire serial interface status register ? twsr  bits 7..3 ? tws: two-wire serial interface status these five bits reflect the status of the two-wire serial interface logic and the two-wire serial bus.  bits 2..0 ? res: reserved bits these bits are reserved in atmega163 and will always read as zero the twsr is read only. it contains a status code which reflects the status of the two- wire serial interface logic and the two-wire serial bus. there are 26 possible status codes. when twsr contains $f8, no relevant state information is available and no two-wire serial interface interrupt is requested. a valid status code is available in twsr one cpu clock cycle after the two-wire serial interface interrupt flag (twint) is set by hardware and is valid until one cpu clock cycle after twint is cleared by soft- ware. table 32 to table 36 give the status information for the various modes. the two-wire serial interface data register ? twdr  bits 7..0 ? twd: two-wire serial interface data register these eight bits constitute the next data byte to be transmitted, or the latest data byte received on the two-wire serial bus. in transmit mode, twdr contains the next byte to be transmitted. in receive mode, the twdr contains the last byte received. it is writeable while the two-wire serial interface is not in the process of shifting a byte. this occurs when the two-wire serial interface interrupt flag (twint) is set by hardware. note that the data register cannot be initial- ized by the user before the first interrupt occurs. the data in twdr remain stable as long as twint is set. while data is shifted out, data on the bus is simultaneously shifted in. twdr always contains the last byte present on the bus, except after a wake up from adc noise reduction mode, power-down mode, or power-save mode by the two-wire serial interface interrupt. for example, in the case of a lost bus arbitration, no data is lost in the transition from master to slave. handling of the ack flag is controlled auto- matically by the two-wire serial interface logic, the cpu cannot access the ack bit directly. bit 76543210 $01 ($21) tws7 tws6 tws5 tws4 tws3 ? ? ? twsr read/write r rrrrrrr initial value11111000 bit 76543210 $03 ($23) twd7 twd6 twd5 twd4 twd3 twd2 twd1 twd0 twdr read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value11111111
85 atmega163(l) 1142e?avr?02/03 the two-wire serial interface (slave) address register ? twar  bits 7..1 ? twa: two-wire serial interface (slave) address register these seven bits constitute the slave address of the two-wire serial bus unit.  bit 0 ? twgce: two-wire serial interface general call recognition enable bit this bit enables, if set, the recognition of the general call given over the two-wire serial bus. the twar should be loaded with the 7-bit slave address (in the seven most significant bits of twar) to which the two-wire serial interface will respond when programmed as a slave transmitter or receiver, and not needed in the master modes. the lsb of twar is used to enable recognition of the general call address ($00). there is an asso- ciated address comparator that looks for the slave address (or generall call address if enabled) in the received serial address. if a match is found, an interrupt request is generated. two-wire serial interface modes the two-wire serial interface can operate in four different modes:  master transmitter  master receiver slave receiver  slave transmitter data transfer in each mode of operation is shown in figure 52 to figure 55. these fig- ures contain the following abbreviations: s: start condition r: read bit (high level at sda) w: write bit (low level at sda) a: acknowledge bit (low level at sda) a : not acknowledge bit (high level at sda) data: 8-bit data byte p: stop condition sla: slave address in figure 52 to figure 55, circles are used to indicate that the two-wire serial interface interrupt flag is set. the numbers in the circles show the status code held in twsr. at these points, actions must be taken by the application to continue or complete the two- wire serial bus transfer. the two-wire serial bus transfer is suspended until the two- wire serial interface interrupt flag is cleared by software. the two-wire serial interface interrupt flag is not automatically cleared by hardware when executing the interrupt routine. software has to clear the flag to continue the two- wire transfer. also note that the two-wire serial interface starts execution as soon as this bit is cleared, so that all access to twar, twdr, and twsr must have been com- pleted before clearing this flag. bit 7654321 0 $02 ($22) twa6 twa5 twa4 twa3 twa2 twa1 twa0 twgce twar read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value1111111 0
86 atmega163(l) 1142e?avr?02/03 when the two-wire serial interface interrupt flag is set, the status code in twsr is used to determine the appropriate software action. for each status code, the required software action and details of the following serial transfer are given in table 32 to table 36. master transmitter mode in the master transmitter mode, a number of data bytes are transmitted to a slave receiver (see figure 52). before master transmitter mode can be entered, the twcr must be initialized as follows: twen must be set to enable the two-wire serial interface, twsta and twsto must be cleared. the master transmitter mode may now be entered by setting the twsta bit. the two- wire serial interface logic will then test t he two-wire serial bus and generate a start condition as soon as the bus becomes free. when a start condition is transmitted, the two-wire serial interface interrupt flag (twint) is set by hardware, and the status code in twsr will be $08. twdr must then be loaded with the slave address and the data direction bit (sla+w). clearing the twint bit in software will continue the transfer. the twint flag is cleared by writing a logic one to the flag. when the slave address and the direction bit have been transmitted and an acknowl- edgement bit has been received, twint is set again and a number of status codes in twsr are possible. possible status codes in master mode are $18, $20, or $38. the appropriate action to be taken for each of these status codes is detailed in table 32. the data must be loaded when twint is high only. if not, the access w ill be discarded, and the write collision bit ? twwc will be set in the twcr register. this scheme is repeated until the last byte is sent and the transfer is ended by generating a stop con- dition or a repeated start condition. a stop condition is generated by setting twsto, a repeated start condition is generated by setting twsta and twsto. after a repeated start condition (state $10) the two-wire serial interface can access the same slave again, or a new slave without transmitting a stop condition. repeated start enables the master to switch between slaves, master transmitter mode and master receiver mode without loosing control over the bus. assembly code illustrating operation of the master transmitter mode is given at the end of the twi section. master receiver mode in the master receiver mode, a number of data bytes are received from a slave trans- mitter (see figure 53). the transfer is initialized as in the master transmitter mode. when the start condition has been transmitted, the twint flag is set by hardware. the software must then load twdr with the 7-bit slave address and the data direction bit (sla+r). the transfe r will then conti nue when the twint flag is cleared by software. when the slave address and the direction bit have been transmitted and an acknowl- edgement bit has been received, twint is set again and a number of status codes in twsr are possible. possible status codes in master mode are $40, $48, or $38. the appropriate action to be taken for each of these status codes is detailed in table 52. received data can be read from the twdr register when the twint flag is set high by hardware. this scheme is repeated until the last byte has been received and a stop condition is transmitted by writing a logic one to the twsto bit in the twcr register. table 29. twcr: master transmitter mode initialization twcr twint twea twsta twsto twwc twen ? twie value 0 x00 01 0 x
87 atmega163(l) 1142e?avr?02/03 after a repeated start condition (state $10), the two-wire serial interface may switch to the master transmitter mode by loading twdr with sla+w or access a new slave as master receiver or transmitter. assembly code illustrating operation of the master receiver mode is given at the end of the twi section. slave receiver mode in the slave receiver mode, a number of data bytes are received from a master trans- mitter (see figure 54). to initiate the slave receiver mode, twar and twcr must be initialized as follows: the upper seven bits are the address to which the two-wire serial interface will respond when addressed by a master. if the lsb is set, the two-wire serial interface will respond to the general call address ($00), otherwise it will ignore the general call address. twen must be set to enable the two-wire serial interface. the twea bit must be set to enable the acknowledgement of the device?s own slave address or the general call address. twsta and twsto must be cleared. when twar and twcr have been initialized, the two-wire serial interface waits until it is addressed by its own slave address (or the general call address if enabled) followed by the data direction bit which must be ?0? (write) for the two-wire serial interface to operate in the slave receiver mode. after its own slave address and the write bit have been received, the two-wire serial interface interrupt flag is set and a valid status code can be read from twsr. the status code is used to determine the appropriate software action. the appropriate action to be taken for each status code is detailed in table 34. the slave receiver mode may also be entered if arbitration is lost while the two-wire serial interface is in the master mode (see states $68 and $78). if the twea bit is reset during a transfer, the two-wire serial interface will return a ?not acknowledge? (?1?) to sda after the next received data byte. while twea is reset, the two-wire serial interface does not respond to its own slave address. however, the two- wire serial bus is still monitored and address recognition may resume at any time by setting twea. this implies that the twea bit may be used to temporarily isolate the two-wire serial interface from the two-wire serial bus. in adc noise reduction mode, power-down mode, and power-save mode, the clock system to the two-wire serial interface is turned off. if the slave receive mode is enabled, the interface can still acknowledge a general call and its own slave address by using the two-wire serial bus clock as a clock source. the part will then wake-up from sleep and the two-wire serial interface will hold the scl clock wil low during the wake- up and until the twint flag is cleared. note that the two-wire serial interface data register ? twdr ? does not reflect the last byte present on the bus when waking up from these sleep modes. assembly code illustrating operation of the slave receiver mode is given at the end of the twi section. table 30. twar: slave receiver mode initialization twar twa6 twa5 twa4 twa3 twa2 twa1 twa0 twgce value device?s own slave address table 31. wcr: slave receiver mode initialization twcr twint twea twsta twsto twwc twen ? twie value 0 100 01 0 x
88 atmega163(l) 1142e?avr?02/03 slave transmitter mode in the slave transmitter mode, a number of data bytes are transmitted to a master receiver (see figure 55). the transfer is initialized as in the slave receiver mode. when twar and twcr have been initialized, the two-wire serial interface waits until it is addressed by its own slave address (or the general call address if enabled) followed by the data direction bit which must be ?1? (read) for the two-wire serial interface to operate in the slave transmitter mode. after its own slave address and the read bit have been received, the two-wire serial interface interrupt flag is set and a valid status code can be read from twsr. the status code is used to determine the appropriate software action. the appropriate action to be taken for each status code is detailed in table 35. the slave transmitter mode may also be entered if arbitration is lost while the two-wire serial interface is in the master mode (see state $b0). if the twea bit is reset during a transfer, the two-wire serial interface will transmit the last byte of the transfer and enter state $c0 or state $c8. the two-wire serial interface is switched to the not addressed slave mode, and will ignore the master if it continues the transfer. thus the master receiver receives all ?1? as serial data. while twea is reset, the two-wire serial interface does not respond to its own slave address. how- ever, the two-wire serial bus is still monitored and address recognition may resume at any time by setting twea. this implies that the twea bit may be used to temporarily isolate the two-wire serial interface from the two-wire serial bus. assembly code illustrating operation of the slave receiver mode is given at the end of the twi section. miscellaneous states there are two status codes that do not correspond to a defined two-wire serial inter- face state, see table 36. status $f8 indicates that no relevant information is available because the two-wire serial interface interrupt flag (twint) is not set yet. this occurs between other states, and when the two-wire serial interface is not involved in a serial transfer. status $00 indicates that a bus error has occured during a two-wire serial bus transfer. a bus error occurs when a start or stop co ndition occurs at an illegal position in the format frame. examples of such illegal positions are during the serial transfer of an address byte, a data byte or an acknowledge bit. when a bus error occurs, twint is set. to recover from a bus error, the twsto flag must set and twint must be cleared by writing a logic one to it. this causes the two-wire serial interface to enter the not addressed slave mode and to clear the twsto flag (no other bits in twcr are affected). the sda and scl lines are released and no stop condition is transmitted.
89 atmega163(l) 1142e?avr?02/03 table 32. status codes for master transmitter mode status code (twsr) status of the two-wire serial bus and two-wire serial inter- face hardware application software response next action taken by two-wire serial interface hard- ware to/from twdr to twcr sta sto twint twea $08 a start condition has been transmitted load sla+w x 0 1 x sla+w will be transmitted; ack or not ack will be received $10 a repeated start condition has been transmitted load sla+w or load sla+r x x 0 0 1 1 x x sla+w will be transmitted; ack or not ack will be received sla+r will be transmitted; logic will switch to master receiver mode $18 sla+w has been transmitted; ack has been received load data byte or no twdr action or no twdr action or no twdr action 0 1 0 1 0 0 1 1 1 1 1 1 x x x x data byte will be transmitted and ack or not ack will be received repeated start will be transmitted stop condition will be transmitted and twsto flag will be reset stop condition followed by a start condition will be transmitted and twsto flag will be reset $20 sla+w has been transmitted; not ack has been received load data byte or no twdr action or no twdr action or no twdr action 0 1 0 1 0 0 1 1 1 1 1 1 x x x x data byte will be transmitted and ack or not ack will be received repeated start will be transmitted stop condition will be transmitted and twsto flag will be reset stop condition followed by a start condition will be transmitted and twsto flag will be reset $28 data byte has been transmitted; ack has been received load data byte or no twdr action or no twdr action or no twdr action 0 1 0 1 0 0 1 1 1 1 1 1 x x x x data byte will be transmitted and ack or not ack will be received repeated start will be transmitted stop condition will be transmitted and twsto flag will be reset stop condition followed by a start condition will be transmitted and twsto flag will be reset $30 data byte has been transmitted; not ack has been received load data byte or no twdr action or no twdr action or no twdr action 0 1 0 1 0 0 1 1 1 1 1 1 x x x x data byte will be transmitted and ack or not ack will be received repeated start will be transmitted stop condition will be transmitted and twsto flag will be reset stop condition followed by a start condition will be transmitted and twsto flag will be reset $38 arbitration lost in sla+w or data bytes no twdr action or no twdr action 0 1 0 0 1 1 x x two-wire serial bus will be released and not addressed slave mode entered a start condition will be transmitted when the bus be- comes free
90 atmega163(l) 1142e?avr?02/03 figure 52. formats and states in the master transmitter mode assembly code example ? master transmitter mode ;the slave being addressed has address 0x64. the code examples also assumes some sort of error handling routine named error. ;part specific include file and twi include file must be included. ; ldi r16, (1< 91 atmega163(l) 1142e?avr?02/03 brne error ldi r16, 0xc8 ; load sla+w into twdr register out twdr, r16 ldi r16, (1< wait4:in r16, twcr ; wait for twint flag set. this indicates that sbrs r16, twint; data has been transmitted, and ack/nack has rjmp wait4 ; been received in r16, twsr ; check value of twi status register. if status cpi r16, mt_data_ack; different from mt_data_ack, go to error brne error ldi r16, (1< 92 atmega163(l) 1142e?avr?02/03 figure 53. formats and states in the master receiver mode table 33. status codes for master receiver mode status code (twsr) status of the two-wire serial bus and two-wire serial inter- face hardware application software response next action taken by two-wire serial interface hard- ware to/from twdr to twcr sta sto twint twea $08 a start condition has been transmitted load sla+r x 0 1 x sla+r will be transmitted ack or not ack will be received $10 a repeated start condition has been transmitted load sla+r or load sla+w x x 0 0 1 1 x x sla+r will be transmitted ack or not ack will be received sla+w will be transmitted logic will switch to master transmitter mode. $38 arbitration lost in sla+r or not ack bit no twdr action or no twdr actio 0 1 0 0 1 1 x x two-wire serial bus will be released and not addressed slave mode will be entered a start condition will be transmitted when the bus becomes free $40 sla+r has been transmitted; ack has been received no twdr action or no twdr action 0 0 0 0 1 1 0 1 data byte will be received and not ack will be returned data byte will be received and ack will be returned $48 sla+r has been transmitted; not ack has been received no twdr action or no twdr action or no twdr action 1 0 1 0 1 1 1 1 1 x x x repeated start will be transmitted stop condition will be transmitted and twsto flag will be reset stop condition followed by a start condition will be transmitted and twsto flag will be reset $50 data byte has been received; ack has been returned read data byte or read data byte 0 0 0 0 1 1 0 1 data byte will be received and not ack will be returned data byte will be received and ack will be returned $58 data byte has been received; not ack has been returned read data byte or read data byte or read data byte 1 0 1 0 1 1 1 1 1 x x x repeated start will be transmitted stop condition will be transmitted and twsto flag will be reset stop condition followed by a start condition will be transmitted and twsto flag will be reset s sla r a data a $08 $40 $50 s sla r $10 ap $48 a or a $38 other master continues $38 other master continues w a $68 other master continues $78 $b0 to corresponding states in slave mode mr mt successfull reception from a slave receiver next transfer started with a repeated start condition not acknowledge received after the slave address arbitration lost in slave address or data byte arbitration lost and addressed as slave data a n from master to slave from slave to master any number of data bytes and their associated acknowledge bits this number (contained in twsr) corresponds to a defined state of the two-wire serial bus p data a $58 a
93 atmega163(l) 1142e?avr?02/03 assembly code example ? master receiver mode ;part specific include file and twi include file must be included. ; ldi r16, (1< ldi r16, (1< ;receive next to last data byte. wait8:in r16,twcr ; wait for twint flag set. this indicates that
94 atmega163(l) 1142e?avr?02/03 sbrs r16, twint ; data has been received and ack returned rjmp wait8 in r16, twsr ; check value of twi status register. if status cpi r16, mr_data_ack ; different from mr_data_ack, go to error brne error in r16, twdr ; input received data from twdr. nop ; ldi r16, (1< ldi r16, (1< 95 atmega163(l) 1142e?avr?02/03 table 34. status codes for slave receiver mode status code (twsr) status of the two-wire serial bus and two-wire serial interface hardware application software response next action taken by two-wire serial interface hardt- ware to/from twdr to twcr sta sto twint twea $60 own sla+w has been received; ack has been returned no twdr action or no twdr action x x 0 0 1 1 0 1 data byte will be received and not ack will be returned data byte will be received and ack will be returned $68 arbitration lost in sla+r/w as master; own sla+w has been received; ack has been returned no twdr action or no twdr action x x 0 0 1 1 0 1 data byte will be received and not ack will be returned data byte will be received and ack will be returned $70 general call address has been received; ack has been returned no twdr action or no twdr action x x 0 0 1 1 0 1 data byte will be received and not ack will be returned data byte will be received and ack will be returned $78 arbitration lost in sla+r/w as master; general call address has been received; ack has been returned no twdr action or no twdr action x x 0 0 1 1 0 1 data byte will be received and not ack will be returned data byte will be received and ack will be returned $80 previously addressed with own sla+w; data has been received; ack has been returned read data byte or read data byte x x 0 0 1 1 0 1 data byte will be received and not ack will be returned data byte will be received and ack will be returned $88 previously addressed with own sla+w; data has been received; not ack has been returned read data byte or read data byte or read data byte or read data byte 0 0 1 1 0 0 0 0 1 1 1 1 0 1 0 1 switched to the not addressed slave mode; no recognition of own sla or gca switched to the not addressed slave mode; own sla will be recognized; gca will be recognized if twgce = ?1? switched to the not addressed slave mode; no recognition of own sla or gca; a start condition will be transmitted when the bus becomes free switched to the not addressed slave mode; own sla will be recognized; gca will be recognized if twgce = ?1?; a start condition will be transmitted when the bus becomes free $90 previously addressed with general call; data has been re- ceived; ack has been returned read data byte or read data byte x x 0 0 1 1 0 1 data byte will be received and not ack will be returned data byte will be received and ack will be returned $98 previously addressed with general call; data has been received; not ack has been returned read data byte or read data byte or read data byte or read data byte 0 0 1 1 0 0 0 0 1 1 1 1 0 1 0 1 switched to the not addressed slave mode; no recognition of own sla or gca switched to the not addressed slave mode; own sla will be recognized; gca will be recognized if twgce = ?1? switched to the not addressed slave mode; no recognition of own sla or gca; a start condition will be transmitted when the bus becomes free switched to the not addressed slave mode; own sla will be recognized; gca will be recognized if twgce = ?1?; a start condition will be transmitted when the bus becomes free $a0 a stop condition or repeated start condition has been received while still addressed as slave read data byte or read data byte or read data byte or read data byte 0 0 1 1 0 0 0 0 1 1 1 1 0 1 0 1 switched to the not addressed slave mode; no recognition of own sla or gca switched to the not addressed slave mode; own sla will be recognized; gca will be recognized if twgce = ?1? switched to the not addressed slave mode; no recognition of own sla or gca; a start condition will be transmitted when the bus becomes free switched to the not addressed slave mode; own sla will be recognized; gca will be recognized if twgce = ?1?; a start condition will be transmitted when the bus becomes free
96 atmega163(l) 1142e?avr?02/03 figure 54. formats and states in the slave receiver mode assembly code example ? slave receiver mode ;part specific include file and twi include file must be included. ; ldi r16, (1< wait10:in r16,twcr ; wait for twint flag set. this indicates that sbrs r16, twint ; start followed by sla+w has been received rjmp wait10 in r16, twsr ; check value of twi status register. if status cpi r16, sr_sla_ack ; different from sr_sla_ack, go to error brne error ldi r16, (1< 97 atmega163(l) 1142e?avr?02/03 ; ack should be returned after receiving first ; data byte wait12:in r16,twcr ; wait for twint flag set. this indicates that sbrs r16, twint ; data has been received and ack returned rjmp wait12 in r16, twsr ; check value of twi status register. if status cpi r16, sr_data_ack ; different from sr_data_ack, go to error brne error in r16, twdr ; input received data from twdr. nop ; ldi r16, (1< ldi r16, (1<
98 atmega163(l) 1142e?avr?02/03 figure 55. formats and states in the slave transmitter mode table 35. status codes for slave transmitter mode status code (twsr) status of the two-wire serial bus and two-wire serial interface hardware application software response next action taken by two-wire serial interface hard- ware to/from twdr to twcr sta sto twint twea $a8 own sla+r has been received; ack has been returned load data byte or load data byte x x 0 0 1 1 0 1 last data byte will be transmitted and not ack should be received data byte will be transmitted and ack should be re- ceived $b0 arbitration lost in sla+r/w as master; own sla+r has been received; ack has been returned load data byte or load data byte x x 0 0 1 1 0 1 last data byte will be transmitted and not ack should be received data byte will be transmitted and ack should be re- ceived $b8 data byte in twdr has been transmitted; ack has been received load data byte or load data byte x x 0 0 1 1 0 1 last data byte will be transmitted and not ack should be received data byte will be transmitted and ack should be re- ceived $c0 data byte in twdr has been transmitted; not ack has been received no twdr action or no twdr action or no twdr action or no twdr action 0 0 1 1 0 0 0 0 1 1 1 1 0 1 0 1 switched to the not addressed slave mode; no recognition of own sla or gca switched to the not addressed slave mode; own sla will be recognized; gca will be recognized if twgce = ?1? switched to the not addressed slave mode; no recognition of own sla or gca; a start condition will be transmitted when the bus becomes free switched to the not addressed slave mode; own sla will be recognized; gca will be recognized if twgce = ?1?; a start condition will be transmitted when the bus becomes free $c8 last data byte in twdr has been transmitted (twea = ?0?); ack has been received no twdr action or no twdr action or no twdr action or no twdr action 0 0 1 1 0 0 0 0 1 1 1 1 0 1 0 1 switched to the not addressed slave mode; no recognition of own sla or gca switched to the not addressed slave mode; own sla will be recognized; gca will be recognized if twgce = ?1? switched to the not addressed slave mode; no recognition of own sla or gca; a start condition will be transmitted when the bus becomes free switched to the not addressed slave mode; own sla will be recognized; gca will be recognized if twgce = ?1?; a start condition will be transmitted when the bus becomes free s sla r a data a $a8 $b8 a $b0 reception of the own slave address and one or more data bytes last data byte transmitted. switched to not addressed slave (twea = "0") arbitration lost as master and addressed as slave n from master to slave from slave to master any number of data bytes and their associated acknowledge bits this number (contained in twsr) corresponds to a defined state of the two-wire serial bus p or s data $c0 data a a $c8 p or s all 1's a
99 atmega163(l) 1142e?avr?02/03 assembly code example ? slave transmitter mode ; part specific include file and twi include file must be included. ; ldi r16, (1< wait14:in r16,twcr ; wait for twint flag set. this indicates that sbrs r16, twint ; sla+r has been received, and ack/nack has rjmp wait14 ; been returned in r16, twsr ; check value of twi status register. if status cpi r16, st_sla_ack; different from st_sla_ack, go to error brne error ldi r16, 0x33 ; load data (here, data = 0x33) into twdr register out twdr, r16 ldi r16, (1< wait15: in r16,twcr ; wait for twint flag set. this indicates that sbrs r16, twint ; data has been transmitted, and ack/nack has rjmp wait15 ; been received in r16, twsr ; check value of twi status register. if status cpi r16, st_data_ack ; different from st_data_ack, go to error brne error ldi r16, 0x44 ; load data (here, data = 0x44) into twdr register out twdr, r16 ldi r16, (1< 100 atmega163(l) 1142e?avr?02/03 ; be received after data byte master signalling end ; of transmission) wait17:in r16,twcr ; wait for twint flag set. this indicates that sbrs r16, twint ; data has been transmitted, and ack/nack has rjmp wait17 ; been received in r16, twsr ; check value of twi status register. if status cpi r16, st_last_data ; different from st_last_data, go to error brne error ldi r16, (1< 101 atmega163(l) 1142e?avr?02/03 .equ st_data_nack =$c0 ;data byte has been tramsmitted and nack ;received .equ st_last_data =$c8 ;last byte in i2dr has been transmitted (twea = ;?0?), ack has been received ;***** slave receiver staus codes ***** .equ sr_sla_ack =$60 ;sla+r has been received and ack returned .equ sr_arb_lost_sla_ack=$68;arbitration lost in sla+r/w as master. own ;sla+r has been received and ack returned .equ sr_gcall_ack =$70 ;generall call has been received and ack ;returned .equ sr_arb_lost_gcall_ack=$78;arbitration lost in sla+r/w as master. ;general call has been received and ack ;returned .equ sr_data_ack =$80 ;previously addressed with own sla+w. data byte ;has been received and ack returned .equ sr_data_nack =$88 ;previously addressed with own sla+w. data byte ;has been received and nack returned .equ sr_gcall_data_ack=$90;previously addressed with general call.data ;byte has been received and ack returned .equ sr_gcall_data_nack=$98;previously addressed with general call. data ;byte has been received and nack returned .equ sr_stop =$a0 ;a stop condition or repeated start condition ;has been received while still addressed as a ;slave ;***** miscellanous states ***** .equ no_info =$f8 ;no relevant state information; twint = ?0? .equ bus_error =$00 ;bus error due to illegal start or stop ;condition
102 atmega163(l) 1142e?avr?02/03 the analog comparator the analog comparator compares the input values on the positive pin pb2 (ain0) and negative pin pb3 (ain1). when the voltage on the positive pin pb2 (ain0) is higher than the voltage on the negative pin pb3 (ain1), the analog comparator output, aco, is set (one). the comparator?s output can be set to trigger the timer/counter1 input capture function. in addition, the comparator can trigger a separate interrupt, exclusive to the analog comparator. the user can select interrupt triggering on comparator output rise, fall or toggle. a block diagram of the comparator and its surrounding logic is shown in figure 56. figure 56. analog comparator block diagram note: 1. see figure 57 on page 106. the analog comparator control and status register ? acsr  bit 7 ? acd: analog comparator disable when this bit is set(one), the power to the analog comparator is switched off. this bit can be set at any time to turn off the analog comparator. this will reduce power con- sumption in active and idle mode. when changing the acd bit, the analog comparator interrupt must be disabled by clearing the acie bit in acsr. otherwise an interrupt can occur when the bit is changed.  bit 6 ? acbg: analog comparator bandgap select when this bit is set and the bod is enabled (boden fuse is programmed), a fixed bandgap voltage of nominally 1.22v replaces the positive input to the analog compara- tor. when this bit is cleared, ain0 is applied to the positive input of the analog comparator. acbg bandgap reference adc multiplexer output acme aden 1) bit 76543210 $08 ($28) acd acbg aco aci acie acic acis1 acis0 acsr read/write r/w r/w r r/w r/w r/w r/w r/w initial value00n/a00000
103 atmega163(l) 1142e?avr?02/03  bit 5 ? aco: analog comparator output aco is directly connected to the comparator output.  bit 4 ? aci: analog comparator interrupt flag this bit is set (one) when a comparator output event triggers the interrupt mode defined by acis1 and acis0. the analog comparator interrupt routine is executed if the acie bit is set (one) and the i-bit in sreg is set (one). aci is cleared by hardware when exe- cuting the corresponding interrupt handling vector. alternatively, aci is cleared by writing a logic one to the flag.  bit 3 ? acie: analog comparator interrupt enable when the acie bit is set (one) and the i-bit in the status register is set (one), the ana- log comparator interrupt is activated. when cleared (zero), the interrupt is disabled.  bit 2 ? acic: analog comparator input capture enable when set (one), this bit enables the input capture function in timer/counter1 to be trig- gered by the analog comparator. the comparator output is in this case directly connected to the input capture front-end logic, making the comparator utilize the noise canceler and edge select features of the timer/counter1 input capture interrupt. when cleared (zero), no connection between the analog comparator and the input capture function is given. to make the comparator trigger the timer/counter1 input capture interrupt, the ticie1 bit in the timer interrupt mask register (timsk) must be set (one).  bits 1, 0 ? acis1, acis0: analog comparator interrupt mode select these bits determine which comparator events that trigger the analog comparator inter- rupt. the different settings are shown in table 37. when changing the acis1/acis0 bits, the analog comparator interrupt must be dis- abled by clearing its interrupt enable bit in the acsr register. otherwise an interrupt can occur when the bits are changed. table 37. acis1/acis0 settings acis1 acis0 interrupt mode 0 0 comparator interrupt on output toggle 01reserved 1 0 comparator interrupt on falling output edge 1 1 comparator interrupt on rising output edge
104 atmega163(l) 1142e?avr?02/03 analog comparator multiplexed input it is possible to select any of the pa7..0 (adc7..0) pins to replace the negative input to the analog comparator. the adc multiplexer is used to select this input, and conse- quently, the adc must be switched off to utilize this feature. if the analog comparator multiplexer enable bit (acme in sfior) is set (one) and the adc is switched off (aden in adcsr is zero), mux2..0 in admux select the input pin to replace the negative input to the analog comparator, as shown in table 38. if acme is cleared (zero) or aden is set (one), pb3 (ain1) is applied to the negative input to the analog comparator. table 38. analog comparator multiplexed input acme aden mux2..0 analog comparator negative input 0x xxxain1 11 xxxain1 1 0 000 adc0 1 0 001 adc1 1 0 010 adc2 1 0 011 adc3 1 0 100 adc4 1 0 101 adc5 1 0 110 adc6 1 0 111 adc7
105 atmega163(l) 1142e?avr?02/03 analog to digital converter feature list  10-bit resolution  0.5 lsb integral non-linearity  2 lsb absolute accuracy  65 - 260 s conversion time  up to 15 ksps at maximum resolution  up to 76 ksps at 8-bit resolution  eight multiplexed single ended input channels  optional left adjustment for adc result readout  0 - v cc adc input voltage range  selectable 2.56v adc reference voltage  free run or single conversion mode  interrupt on adc conversion complete  sleep mode noise canceler the atmega163 features a 10-bit successive approximation adc. the adc is con- nected to an 8-channel analog multiplexer which allows each pin of port a to be used as input for the adc. the adc contains a sample and hold amplifier which ensures that the input voltage to the adc is held at a constant level during conversion. a block diagram of the adc is shown in figure 57. the adc has two separate analog supply voltage pins, avcc and agnd. agnd must be connected to gnd, and the voltage on avcc must not differ more than 0.3v from v cc . see the paragraph adc noise canceling techniques on how to connect these pins. internal reference voltages of nominally 2.56v or avcc are provided on-chip. the 2.56v reference may be externally decoupled at the aref pin by a capacitor for better noise perfomance. see ?internal voltage reference? on page 29 for a description of the internal voltage reference.
106 atmega163(l) 1142e?avr?02/03 figure 57. analog to digital converter block schematic operation the adc converts an analog input voltage to a 10-bit digital value through successive approximation. the minimum value represents agnd and the maximum value repre- sents the voltage on the aref pin minus 1 lsb. optionally, avcc or an internal 2.56v reference voltage may be connected to the aref pin by writing to the refsn bits in the admux register. the internal voltage reference may thus be decoupled by an external capacitor at the aref pin to improve noise immunity. the analog input channel is selected by writing to the mux bits in admux. any of the eight adc input pins adc7..0, as well as agnd and a fixed bandgap voltage reference of nominally 1.22v (v bg ), can be selected as single ended inputs to the adc. the adc can operate in two modes ? single conversion and free running mode. in single conversion mode, each conversion will have to be initiated by the user. in free running mode, the adc is constantly sampling and updating the adc data register. the adfr bit in adcsr selects between the two available modes. the adc is enabled by setting the adc enable bit, aden in adcsr. voltage reference and input channel selections will not go in to effect until aden is set. the adc does not adc conversion complete irq 8-bit data bus 15 0 adc multiplexer select (admux) adc ctrl. & status register (adcsr) adc data register (adch/adcl) mux2 adie adfr adsc aden adif adif mux1 mux0 adps0 adps1 adps2 conversion logic 10-bit dac + - sample & hold comparator internal 2.56 v reference mux decoder avcc refs0 refs1 adlar channel selection adc[9:0] adc multiplexer output aref prescaler adc7 adc6 adc5 adc4 adc3 adc2 adc1 adc0 1.22 v bandgap reference agnd input mux mux3 mux4
107 atmega163(l) 1142e?avr?02/03 consume power when aden is cleared, so it is recommended to switch off the adc before entering power saving sleep modes. a conversion is started by writing a logical one to the adc start conversion bit, adsc. this bit stays high as long as the conversion is in progress and will be set to zero by hardware when the conversion is completed. if a different data channel is selected while a conversion is in progress, the adc will fi nish the current conversion before performing the channel change. the adc generates a 10-bit result, which are presented in the adc data registers, adch and adcl. by default, the result is presented right adjusted, but can optionally be presented left adjusted by setting the adlar bit in admux. if the result is left adjusted and no more than 8-bit precision is required, it is sufficient to read adch. otherwise, adcl must be read fi rst, then adch, to ensure that the content of the data registers belongs to the same conversion. once adcl is read, adc access to data registers is blocked. this means that if adcl has been read, and a conversion completes before adch is read, neither register is updated and the result from the con- version is lost. when adch is read, adc access to the adch and adcl registers is re-enabled. the adc has its own interrupt which can be triggered when a conversion completes. when adc access to the data registers is prohibited between reading of adch and adcl, the interrupt will trigger even if the result is lost. prescaling and conversion timing figure 58. adc prescaler the successive approximation circuitry requires an input clock frequency between 50 khz and 200 khz to achieve maximum resolution. if a lower resolution than 10 bits is required, the input clock frequency to the adc can be higher than 200 khz to achieve a higher sampling rate. see ?adc characteristics? on page 114 for more details. the adc module contains a prescaler, which divides the system clock to an acceptable adc clock frequency. the adps bits in adcsr are used to generate a proper adc clock input frequency from any xtal frequency above 100 khz. the prescaler starts counting from the moment the adc is switched on by setting the aden bit in adcsr. the prescaler keeps running for as long as the aden bit is set, and is continuously reset when aden is low. 7-bit adc prescaler adc clock source ck adps0 adps1 adps2 ck/128 ck/2 ck/4 ck/8 ck/16 ck/32 ck/64 reset aden
108 atmega163(l) 1142e?avr?02/03 when initiating a conversion by setting the adsc bit in adcsr, the conversion starts at the following rising edge of the adc clock cycle. a normal conversion takes 13 adc clock cycles. in certain situations, the adc needs more clock cycles to initalization and minimi ze offset errors. extended conversions take 25 adc clock cycles and occur as the first conversion after the adc is switched on (aden in adcsr is set). additionally, when changing voltage reference, the user may improve accuracy by disregarding the first conversion result after the reference or mux setting was changed. the actual sample-and-hold takes place 1.5 adc clock cycles after the start of a normal conversion and 13.5 adc clock cycles after the start of an extended conversion. when a conversion is complete, the result is written to the adc data registers, and adif is set. in single conversion mode, adsc is cleared simultaneously. the software may then set adsc again, and a new conversion will be initated on the first rising adc clock edge. in free running mode, a new conversi on will be started immediately after the conversion completes, while adsc remains high. using free running mode and an adc clock frequency of 200 khz gives the lowest conversion time with a maximum res- olution, 65 s, equivalent to 15 ksps. for a summary of conversion times, see table 39. figure 59. adc timing diagram, extended conversion (single conversion mode) figure 60. adc timing diagram, single conversion sign and msb of result lsb of result adc clock adsc sample & hold adif adch adcl cycle number aden 1 212 13 14 15 16 17 18 19 20 21 22 23 24 25 1 2 extended conversion next conversion 3 mux and refs update mux and refs update conversion complete 1 2 3 4 5 6 7 8 9 10 11 12 13 sign and msb of result lsb of result adc clock adsc adif adch adcl cycle number 12 one conversion next conversion 3 sample & hold mux and refs update conversion complete mux and refs update
109 atmega163(l) 1142e?avr?02/03 figure 61. adc timing diagram, free run conversion adc noise canceler function the adc features a noise canceler that enables conversion during adc noise reduc- tion mode (see ?sleep modes? on page 35) to reduce noise induced from the cpu core and other i/o peripherals. if other i/o peripherals must be active during conversion, this mode works equivalently for idle mode. to make use of this feature, the following proce- dure should be used: 1. make sure that the adc is enabled and is not busy converting. single conver- sion mode must be selected and the adc conversion complete interrupt must be enabled. aden = 1 adsc = 0 adfr = 0 adie = 1 2. enter adc noise reduction mode (or idle mode). the adc will start a conver- sion once the cpu has been halted. 3. if no other interrupts occur before the adc conversion completes, the adc inter- rupt will wake up the cpu and execute the adc conversion complete interrupt routine. table 39. adc conversion time condition sample & hold (cycles from start of conversion) conversion time (cycles) conversion time (s) extended conversion 13.5 25 125 - 500 normal conversions 1.5 13 65 - 260 11 12 13 sign and msb of result lsb of result adc clock adsc adif adch adcl cycle number 12 one conversion next conversion 34 conversion complete sample & hold mux and refs update
110 atmega163(l) 1142e?avr?02/03 the adc multiplexer selection register ? admux  bit 7, 6 ? refs1..0: reference selection bits these bits select the voltage reference for the adc, as shown in table 17. if these bits are changed during a conversion, the change will not go in effect until this conversion is complete (adif in adcsr is set). the user should disregard the first conversion result after changing these bits to obtain maximum accuracy. the internal voltage reference options may not be used if an external reference voltage is being applied to the aref pin.  bit 5 ? adlar: adc left adjust result the adlar bit affects the presentation of the adc conversion result in the adc data register. if adlar is cleared, the result is right adjusted. if adlar is set, the result is left adjusted. changing the adlar bit will affect the adc data register immediately, regardless of any ongoing conversions. for a complete description of this bit, see ?the adc data register ? adcl and adch? on page 112.  bits 4..0 ? mux4..mux0: analog channel and gain selection bits the value of these bits selects which combination of analog inputs are connected to the adc. see table 41 for details. if these bits are changed during a conversion, the change will not go in effect until this conversion is complete (adif in adcsr is set). bit 76543210 $07 ($27) refs1 refs0 adlar mux4 mux3 mux2 mux1 mux0 admux read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000 table 40. voltage reference selections for adc refs1 refs0 voltage reference selection 0 0 aref, internal vref turned off 0 1 avcc with external capacitor at aref pin 10reserved 1 1 internal 2.56v voltage reference with external capacitor at aref pin table 41. input channel selections mux4..0 single-ended input 00000 adc0 00001 adc1 00010 adc2 00011 adc3 00100 adc4 00101 adc5 00110 adc6 00111 adc7
111 atmega163(l) 1142e?avr?02/03 the adc control and status register ? adcsr  bit 7 ? aden: adc enable writing a logical ?1? to this bit enables the adc. by clearing this bit to zero, the adc is turned off. turning the adc off while a conv ersion is in progress, will terminate this conversion.  bit 6 ? adsc: adc start conversion in single conversion mode, a logical ?1? must be written to this bit to start each conver- sion. in free running mode, a logical ?1? must be written to this bit to start the first conversion. the first time adsc has been written after the adc has been enabled, or if adsc is written at the same time as the adc is enabled, an extended conversion will precede the initiated conversion. this extended conversion performs initialization of the adc. adsc will read as one as long as a conversion is in progress. when the conversion is complete, it returns to zero. when a extended conversion precedes a real conversion, adsc will stay high until the real conversion completes. writing a 0 to this bit has no effect.  bit 5 ? adfr: adc free running select when this bit is set (one) the adc operates in free running mode. in this mode, the adc samples and updates the data registers continuously. clearing this bit (zero) will terminate free running mode.  bit 4 ? adif: adc interrupt flag this bit is set (one) when an adc conversion completes and the data registers are updated. the adc conversion complete interrupt is executed if the adie bit and the i- bit in sreg are set (one). adif is cleared by hardware when executing the correspond- ing interrupt handling vector. alternatively, adif is cleared by writing a logical one to the flag. beware that if doing a read-modify-write on adcsr, a pending interrupt can be disabled. this also applies if the sbi and cbi instructions are used.  bit 3 ? adie: adc interrupt enable when this bit is set (one) and the i-bit in sreg is set (one), the adc conversion com- plete interrupt is activated. 01000..11101 reserved 11110 1.22v (v bg ) 11111 0v (agnd) table 41. input channel selections (continued) mux4..0 single-ended input bit 76543210 $06 ($26) aden adsc adfr adif adie adps2 adps1 adps0 adcsr read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000
112 atmega163(l) 1142e?avr?02/03  bits 2..0 ? adps2..0: adc prescaler select bits these bits determine the division factor between the xtal frequency and the input clock to the adc. the adc data register ? adcl and adch adlar = 0 adlar = 1 when an adc conversion is complete, the result is found in these two registers. when adcl is read, the adc data register is not updated until adch is read. conse- quently, if the result is left adjusted and no more than 8-bit precision is required, it is sufficient to read adch. otherwise, adcl must be read first, then adch. the adlar bit in admux affects the way the result is read from the registers. if adlar is set, the result is left adjusted. if adlar is cleared (default), the result is right adjusted.  adc9..0: adc conversion result these bits represent the result from the conversion. $000 represents analog ground, and $3ff represents the selected reference voltage minus one lsb. table 42. adc prescaler selections adps2 adps1 adps0 division factor 000 2 001 2 010 4 011 8 100 16 101 32 110 64 111 128 bit 151413121110 9 8 $05 ($25) sign ? ? ? ? ? adc9 adc8 adch $04 ($24) adc7 adc6 adc5 adc4 adc3 adc2 adc1 adc0 adcl 76543210 read/write r r r r r r r r rrrrrrrr initial value00000000 00000000 bit 151413121110 9 8 $05 ($25) adc9 adc8 adc7 adc6 adc5 adc4 adc3 adc2 adch $04 ($24) adc1 adc0 ? ? ? ? ? ? adcl 76543210 read/write r r r r r r r r rrrrrrrr initial value00000000 00000000
113 atmega163(l) 1142e?avr?02/03 scanning multiple channels since change of analog channel always is delayed until a conversion is finished, the free running mode can be used to scan multiple channels without interrupting the con- verter. typically, the adc conversion complete interrupt will be used to perform the channel shift. however, the user should take the following fact into consideration: the interrupt triggers once the result is ready to be read. in free running mode, the next conversion will start immediately when the interrupt triggers. if admux is changed after the interrupt triggers, the next conversion has already started, and the old setting is used. adc noise canceling techniques digital circuitry inside and outside the atmega163 generates emi which might affect the accuracy of analog measurements. if conversion accuracy is critical, the noise level can be reduced by applying the following techniques: 1. the analog part of the atmega163 and all analog components in the application should have a separate analog ground plane on the pcb. this ground plane is connected to the digital ground plane via a single point on the pcb. 2. keep analog signal paths as short as possi ble. make sure analog tracks run over the analog ground plane, and keep them well away from high-speed switching digital tracks. 3. the avcc pin on the atmega163 should be connected to the digital v cc supply voltage via an lc network as shown in figure 62. 4. use the adc noise canceler function to reduce induced noise from the cpu. 5. if some port a pins are used as digital outputs, it is essential that these do not switch while a conversion is in progress. figure 62. adc power connections gnd vcc pa0 (adc0) pa1 (adc1) pa2 (adc2) pa3 (adc3) pa4 (adc4) pa5 (adc5) pa6 (adc6) pa7 (adc7) aref avcc agnd pc7 (tosc2) 10 ? 100nf analog ground plane atmega163 33 29 31 27 26 32 28 30 37 35 36 34 38 39
114 atmega163(l) 1142e?avr?02/03 adc characteristics notes: 1. minimum for avcc is 2.7v. 2. maximum for avcc is 5.5v. table 43. adc characteristics symbol parameter condition min typ max units resolution single-ended conversion 10 bits absolute accuracy v ref = 4v adc clock = 200 khz 12lsb absolute accuracy v ref = 4v adc clock = 1 mhz 4lsb absolute accuracy v ref = 4v adc clock = 2 mhz 16 lsb integral non-linearity v ref > 2v 0.5 lsb differential non-linearity v ref > 2v 0.5 lsb zero error (offset) v ref > 2v 1 lsb conversion time free running conversion 65 260 s clock frequency 50 200 khz av cc analog supply voltage v cc - 0.3 (1) v cc + 0.3 (2) v v ref reference voltage 2 v av cc v vint internal voltage reference 2.35 2.56 2.77 v v bg bandgap voltage reference 1.12 1.22 1.32 v r ref reference input resistance 6 10 13 k ? v in input voltage agnd aref v r ain analog input resistance 100 m ?
115 atmega163(l) 1142e?avr?02/03 i/o ports all avr ports have true read-modify-write functionality when used as general digital i/o ports. this means that the direction of one port pin can be changed without uninten- tionally changing the direction of any other pin with the sbi and cbi instructions. the same applies for changing drive value (if configured as output) or enabling/disabling of pull-up resistors (if configured as input). port a port a is an 8-bit bi-directional i/o port with internal pull-ups. three i/o memory address locations are allocated for port a, one each for the data register ? porta, $1b($3b), data direction register ? ddra, $1a($3a) and the port a input pins ? pina, $19($39). the port a input pins address is read only, while the data register and the data direction register are read/write. all port pins have individually selectable pull-up resistors. the port a output buffers can sink 20 ma and thus drive led displays directly. when pins pa0 to pa7 are used as inputs and are externally pulled low, they will source current if the internal pull-up resis- tors are activated. port a has an alternate function as analog inputs for the adc. if some port a pins are configured as outputs, it is essential that these do not switch when a conversion is in progress. this might corrupt the result of the conversion. during power-down mode, the schmitt trigger of the digital input is disconnected. this allows analog signals that are close to v cc /2 to be present during powerdown without causing excessive power consumption. the port a data register ? porta the port a data direction register ? ddra the port a input pins address ? pina the port a input pins address ? pina ? is not a register, and this address enables access to the physical value on each port a pin. when reading porta the porta data latch is read, and when reading pina, the logical values present on the pins are read. port a as general digital i/o all 8 bits in port a are equal when used as digital i/o pins. pan, general i/o pin: the ddan bit in the ddra register selects the direction of this pin, if ddan is set (one), pan is configured as an output pin. if ddan is cleared (zero), pan is configured as an input pin. if portan is set (one) when the pin configured as an input pin, the mos pull up resistor is activated. to switch the pull up resistor o ff, the portan has to be cleared (zero), the pin has to be configured as an output pin, or the bit 76543210 $1b ($3b) porta7 porta6 porta5 porta4 porta3 porta2 porta1 porta0 porta read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000 bit 76543210 $1a ($3a) dda7 dda6 dda5 dda4 dda3 dda2 dda1 dda0 ddra read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000 bit 76543210 $19 ($39) pina7 pina6 pina5 pina4 pina3 pina2 pina1 pina0 pina read/write r r r rrrrr initial value n/a n/a n/a n/a n/a n/a n/a n/a
116 atmega163(l) 1142e?avr?02/03 pud bit has to be set. the port a pins are tri-stated when a reset condition becomes active, even if the clock is not running. note: 1. n: 7,6?0, pin number. port a schematics note that all port pins are synchronized. the synchronization latches are not shown in the figure. figure 63. porta schematic diagrams (pins pa0 - pa7) table 44. ddan effects on porta pins (1) ddan portan pud i/o pull up comment 0 0 x input no tri-state (hi-z) 0 1 1 input no tri-state (hi-z) 0 1 0 input yes pan will source current if ext. pulled low. 1 0 x output no push-pull zero output 1 1 x output no push-pull one output da ta bus d d q q reset reset c c wd wp rd mos pull- up pdn adcn to adc mux wp: wd: rl: rp: rd: n: write porta write ddra read porta latch read porta pin read ddra 0-7 ddan portan rl rp pwrdn pud pud: pull-up disable
117 atmega163(l) 1142e?avr?02/03 port b port b is an 8-bit bi-directional i/o port with internal pull-ups. three i/o memory address locations are allocated for port b, one each for the data register ? portb, $18($38), data direction register ? ddrb, $17($37) and the port b input pins ? pinb, $16($36). the port b input pins address is read only, while the data register and the data direction register are read/write. all port pins have individually selectable pull-up resistors. the port b output buffers can sink 20 ma and thus drive led displays directly. when pins pb0 to pb7 are used as inputs and are externally pulled low, they will source current if the internal pull-up resis- tors are activated. the port b pins with alternate functions are shown in table 45. when the pins are used for the alternate function, the ddrb and portb registers have to be set according to the alternate function description. the port b data register ? portb the port b data direction register ? ddrb the port b input pins address ? pinb the port b input pins address ? pinb ? is not a register, and this address enables access to the physical value on each port b pin. when reading portb, the portb data latch is read, and when reading pinb, the logical values present on the pins are read. table 45. port b pins alternate functions port pin alternate functions pb0 t0 (timer/counter0 external counter input) pb1 t1 (timer/counter1 external counter input) pb2 ain0 (analog comparator positive input) pb3 ain1 (analog comparator negative input) pb4 ss (spi slave select input) pb5 mosi (spi bus master output/slave input) pb6 miso (spi bus master input/slave output) pb7 sck (spi bus serial clock) bit 76543210 $18 ($38) portb7 portb6 portb5 portb4 portb3 portb2 portb1 portb0 portb read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000 bit 76543210 $17 ($37) ddb7 ddb6 ddb5 ddb4 ddb3 ddb2 ddb1 ddb0 ddrb read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000 bit 76543210 $16 ($36) pinb7 pinb6 pinb5 pinb4 pinb3 pinb2 pinb1 pinb0 pinb read/write r r r rrrrr initial value n/a n/a n/a n/a n/a n/a n/a n/a
118 atmega163(l) 1142e?avr?02/03 port b as general digital i/o all eight bits in port b are equal when used as digital i/o pins. pbn, general i/o pin: the ddbn bit in the ddrb register selects the direction of this pin, if ddbn is set (one), pbn is configured as an output pin. if ddbn is cleared (zero), pbn is configured as an input pin. if portbn is set (one) when the pin configured as an input pin, the mos pull up resistor is activated. to switch the pull up resistor off, the portbn has to be cleared (zero), the pin has to be configured as an output pin, or the pud bit has to be set. the port b pins are tri-stated when a reset condition becomes active, even if the clock is not running. note: 1. n: 7,6?0, pin number. alternate functions of portb the alternate pin configuration is as follows:  sck ? portb, bit 7 sck: master clock output, slave clock input pin for spi channel. when the spi is enabled as a slave, this pin is configured as an input regardless of the setting of ddb7. when the spi is enabled as a master, the data direction of this pin is controlled by ddb7. when the pin is forced to be an input, the pull-up can still be controlled by the portb7 bit. see the description of the spi port for further details.  miso ? portb, bit 6 miso: master data input, slave data output pin for spi channel. when the spi is enabled as a master, this pin is configured as an input regardless of the setting of ddb6. when the spi is enabled as a slave, the data direction of this pin is controlled by ddb6. when the pin is forced to be an input, the pull-up can still be controlled by the portb6 bit. see the description of the spi port for further details.  mosi ? portb, bit 5 mosi: spi master data output, slave data input for spi channel. when the spi is enabled as a slave, this pin is configured as an input regardless of the setting of ddb5. when the spi is enabled as a master, the data direction of this pin is controlled by ddb5. when the pin is forced to be an input, the pull-up can still be controlled by the portb5 bit. see the description of the spi port for further details. ss ? portb, bit 4 ss : slave port select input. when the spi is enabled as a slave, this pin is configured as an input regardless of the setting of ddb4. as a slave, the spi is activated when this pin is driven low. when the spi is enabled as a master, the data direction of this pin is controlled by ddb4. when the pin is forced to be an input, the pull-up can still be con- trolled by the portb4 bit. see the description of the spi port for further details. table 46. ddbn effects on port b pins (1) ddbn portbn pud i/o pull up comment 0 0 x input no tri-state (hi-z) 0 1 1 input no tri-state (hi-z) 0 1 0 input yes pbn will source current if ext. pulled low. 1 0 x output no push-pull zero output 1 1 x output no push-pull one output
119 atmega163(l) 1142e?avr?02/03  ain1 ? portb, bit 3 ain1, analog comparator negative input. when configured as an input (ddb3 is cleared (zero)) and with the internal mos pull up resistor switched off (pb3 is cleared (zero)), this pin also serves as the negative input of the on-chip analog comparator. during power-down mode, the schmitt trigger of the digital input is disconnected. this allows analog signals which are close to v cc /2 to be present during power-down without causing excessive power consumption.  ain0 ? portb, bit 2 ain0, analog comparator positive input. when configured as an input (ddb2 is cleared (zero)) and with the internal mos pull up resistor switched off (pb2 is cleared (zero)), this pin also serves as the positive input of the on-chip analog comparator. during power-down mode, the schmitt trigger of the digital input is disconnected. this allows analog signals which are close to v cc /2 to be present during power-down without caus- ing excessive power consumption.  t1 ? portb, bit 1 t1, timer/counter1 counter source. see the timer description for further details.  t0 ? portb, bit 0 t0: timer/counter0 counter source. see the timer description for further details. port b schematics note that all port pins are synchronized. the synchronization latches are not shown in the figures. figure 64. portb schematic diagram (pins pb0 and pb1) 2 pud pud: pull-up disable
120 atmega163(l) 1142e?avr?02/03 figure 65. portb schematic diagram (pins pb2 and pb3) figure 66. portb schematic diagram (pin pb4) da ta bus d d q q reset reset c c wd wp rd mos pull- up pbn ainm to comparator wp: wd: rl: rp: rd: n: m: write portb write ddrb read portb latch read portb pin read ddrb 2, 3 0, 1 pwrdn ddbn portbn rl rp pud pud: pull-up disable data bus d d q q reset reset c c wd wp rd mos pull- up pb4 spi ss mstr spe wp: wd: rl: rp: rd: mstr: spe: write portb write ddrb read portb latch read portb pin read ddrb spi master enable spi enable ddb4 portb4 rl rp pud pud: pull-up disable
121 atmega163(l) 1142e?avr?02/03 figure 67. portb schematic diagram (pin pb5) figure 68. portb schematic diagram (pin pb6) data bus d d q q reset reset c c wd wp rd mos pull- up pb5 r r wp: wd: rl: rp: rd: spe: mstr: write portb write ddrb read portb latch read portb pin read ddrb spi enable master select ddb5 portb5 spe mstr spi master out spi slave in rl rp pud pud: pull-up disable da ta bus d d q q reset reset c c wd wp rd mos pull- up pb6 r r wp: wd: rl: rp: rd: spe: mstr write portb write ddrb read portb latch read portb pin read ddrb spi enable master select ddb6 portb6 spe mstr spi slave out spi master in rl rp pud pud: pull-up disable
122 atmega163(l) 1142e?avr?02/03 figure 69. portb schematic diagram (pin pb7) da ta bus d d q q reset reset c c wd wp rd mos pull- up pb7 r r wp: wd: rl: rp: rd: spe: mstr write portb write ddrb read portb latch read portb pin read ddrb spi enable master select ddb7 portb7 spe mstr spi cllock out spi clock in rl rp pud pud: pull-up disable
123 atmega163(l) 1142e?avr?02/03 port c port c is an 8-bit bi-directional i/o port with internal pull-ups. three i/o memory address locations are allocated for the port c, one each for the data register ? portc, $15($35), data direction register ? ddrc, $14($34) and the port c input pins ? pinc, $13($33). the port c input pins address is read only, while the data register and the data direction register are read/write. all port pins have individually selectable pull-up resistors. the port c output buffers can sink 20 ma and thus drive led displays directly. when pins pc0 to pc7 are used as inputs and are externally pulled low, they will source current if the internal pull-up resistors are activated. the port c data register ? portc the port c data direction register ? ddrc the port c input pins address ? pinc the port c input pins address ? pinc ? is not a register, and this address enables access to the physical value on each port c pin. when reading portc, the portc data latch is read, and when reading pinc, the logical values present on the pins are read. port c as general digital i/o all eight bits in port c are equal when used as digital i/o pins. pcn, general i/o pin: the ddcn bit in the ddrc register selects the direction of this pin, if ddcn is set (one), pcn is configured as an output pin. if ddcn is cleared (zero), pcn is configured as an input pin. if portcn is set (one) when the pin configured as an input pin, the mos pull up resistor is activated. to switch the pull up resistor off, portcn has to be cleared (zero), the pin has to be configured as an output pin, or the pud bit has to be set. the port c pins are tri-stated when a reset condition becomes active, even if the clock is not running. table 47. port c pins alternate functions port pin alternate function pc0 scl (two-wire serial bus clock line) pc1 sda (two-wire serial bus data input/output line) pc6 tosc1 (timer oscillator pin 1) pc7 tosc2 (timer oscillator pin 2) bit 76543210 $15 ($35) portc7 portc6 portc5 portc4 portc3 portc2 portc1 portc0 portc read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000 bit 76543210 $14 ($34) ddc7 ddc6 ddc5 ddc4 ddc3 ddc2 ddc1 ddc0 ddrc read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000 bit 76543210 $13 ($33) pinc7 pinc6 pinc5 pinc4 pinc3 pinc2 pinc1 pinc0 pinc read/write r r r rrrrr initial value n/a n/a n/a n/a n/a n/a n/a n/a
124 atmega163(l) 1142e?avr?02/03 note: 1. n: 7?0, pin number alternate functions of portc  tosc2 ? portc, bit 7 tosc2, timer oscillator pin 2: when the as2 bit in assr is set (one) to e nable asyn- chronous clocking of timer/counter2, pin pc7 is disconnected from the port, and becomes the inverting output of the oscillator amplifier. in this mode, a crystal oscillator is connected to this pin, and the pin can not be used as an i/o pin.  tosc1 ? portc, bit 6 tosc1, timer oscillator pin 1: when the as2 bit in assr is set (one) to e nable asyn- chronous clocking of timer/counter1, pin pc6 is disconnected from the port, and becomes the input of the inverting oscillator amplifier. in this mode, a crystal oscillator is connected to this pin, and the pin can not be used as an i/o pin.  sda ? portc, bit 1 sda, two-wire serial bus data: when the twen bit in twcr is set (one) to enable the two-wire serial interface, pin pc1 is disconnected from the port and becomes the serial data i/o pin for the two-wire serial interface. in this mode, there is a spike filter on the pin to capture spikes shorter than 50 ns on the input signal, and the pin is driven by an open collector driver with slew rate limitation.  scl ? portc, bit 0 scl, two-wire serial interface clock: when the twen bit in twcr is set (one) to enable the two-wire serial interface, pin pc1 is disconnected from the port and becomes the serial clock i/o pin for the two-wire serial interface. in this mode, there is a spike filter on the pin to capture spikes shorter than 50 ns on the input signal. table 48. ddcn effects on port c pins (1) ddcn portcn pud i/o pull up comment 0 0 x input no tri-state (hi-z) 0 1 1 input no tri-state (hi-z) 0 1 0 input yes pcn will source current if ext. pulled low. 1 0 x output no push-pull zero output 1 1 x output no push-pull one output
125 atmega163(l) 1142e?avr?02/03 port c schematics note that all port pins are synchronized. the synchronization latches are not shown in the figure. figure 70. portc schematic diagram (pins pc0 - pc1) 0 1 twen scl/sda in scl/sda out pcn ddcn n n = 0, 1 pud 0 1 pud: pull-up disable
126 atmega163(l) 1142e?avr?02/03 figure 71. portc schematic diagram (pins pc2 - pc5) figure 72. portc schematic diagram (pins pc6) da ta bus d d q q reset reset c c wd wp rd mos pull- up pcn r r wp: wd: rl: rp: rd: write portc write ddrc read portc latch read portc pin read ddrc ddcn portcn rl rp pud pud: pull-up disable n: 2..5 da ta bus d d q q reset reset c c wd wp rd mos pull- up pc6 r r wp: wd: rl: rp: rd: as2: write portc write ddrc read portc latch read portc pin read ddrc asynch select t/c2 ddc6 portc6 rl rp as2 t/c2 osc amp input pud 0 1 pud: pull-up disable
127 atmega163(l) 1142e?avr?02/03 figure 73. portc schematic diagram (pins pc7) pud 0 1 pud: pull-up disable
128 atmega163(l) 1142e?avr?02/03 port d port d is an 8 bit bi-directional i/o port with internal pull-up resistors. three i/o memory address locations are allocated for port d, one each for the data register ? portd, $12($32), data direction register ? ddrd, $11($31) and the port d input pins ? pind, $10($30). the port d input pins address is read only, while the data register and the data direction register are read/write. the port d output buffers can sink 20 ma. as inputs, port d pins that are externally pulled low will source current if the pull-up resistors are activated. some port d pins have alternate functions as shown in table 49. the port d data register ? portd the port d data direction register ? ddrd the port d input pins address ? pind the port d input pins address ? pind ? is not a register, and this address enables access to the physical value on each port d pin. when reading portd, the portd data latch is read, and when reading pind, the logical values present on the pins are read. table 49. port d pins alternate functions port pin alternate function pd0 rxd (uart input pin) pd1 txd (uart output pin) pd2 int0 (external interrupt 0 input) pd3 int1 (external interrupt 1 input) pd4 oc1b (timer/counter1 output compareb match output) pd5 oc1a (timer/counter1 output comparea match output) pd6 icp (timer/counter1 input capture pin) pd7 oc2 (timer/counter2 output compare match output) bit 76543210 $12 ($32) portd7 portd6 portd5 portd4 portd3 portd2 portd1 portd0 portd read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000 bit 76543210 $11 ($31) ddd7 ddd6 ddd5 ddd4 ddd3 ddd2 ddd1 ddd0 ddrd read/write r/w r/w r/w r/w r/w r/w r/w r/w initial value00000000 bit 76543210 $10 ($30) pind7 pind6 pind5 pind4 pind3 pind2 pind1 pind0 pind read/write r r r rrrrr initial value n/a n/a n/a n/a n/a n/a n/a n/a
129 atmega163(l) 1142e?avr?02/03 port d as general digital i/o pdn, general i/o pin: the dddn bit in the ddrd register selects the direction of this pin. if dddn is set (one), pdn is configured as an output pin. if dddn is cleared (zero), pdn is configured as an input pin. if pdn is set (one) when configured as an input pin the mos pull up resistor is activated. to switch the pull up resistor off the pdn has to be cleared (zero), the pin has to be configured as an output pin, or the pud bit has to be set. the port d pins are tri-stated when a reset condition becomes active, even if the clock is not running. note: 1. n: 7,6?0, pin number. alternate functions of portd  oc2 ? portd, bit 7 oc2, timer/counter2 output compare match output: the pd7 pin can serve as an external output for the timer/counter2 output compare. the pin has to be configured as an output (ddd7 set (one)) to serve this function. see the timer description on how to enable this function. the oc2 pin is also the output pin for the pwm mode timer function.  icp ? portd, bit 6 icp ? input capture pin: the pd6 pin can act as an input capture pin for timer/counter1. the pin has to be configured as an input (ddd6 cleared(zero)) to serve this function. see the timer description on how to enable this function.  oc1a ? portd, bit 5 oc1a, output compare match a output: the pd5 pin can serve as an external output for the timer/counter1 output compare a. the pin has to be configured as an output (ddd5 set (one)) to serve this function. see the timer description on how to enable this function. the oc1a pin is also the output pin for the pwm mode timer function.  oc1b ? portd, bit 4 oc1b, output compare match b output: the pd4 pin can serve as an external output for the timer/counter1 output compare b. the pin has to be configured as an output (ddd4 set (one)) to serve this function. see the timer description on how to enable this function. the oc1b pin is also the output pin for the pwm mode timer function.  int1 ? portd, bit 3 int1, external interrupt source 1: the pd3 pin can serve as an external interrupt source to the mcu. see the interrupt description for further details, and how to enable the source. table 50. dddn bits on port d pins (1) dddn portdn pud i/o pull up comment 0 0 x input no tri-state (hi-z) 0 1 1 input no tri-state (hi-z) 0 1 0 input yes pdn will source current if ext. pulled low. 1 0 x output no push-pull zero output 1 1 x output no push-pull one output
130 atmega163(l) 1142e?avr?02/03  int0 ? portd, bit 2 int0, external interrupt source 0: the pd2 pin can serve as an external interrupt source to the mcu. see the interrupt description for further details, and how to enable the source.  txd ? port d, bit 1 txd, transmit data (data output pin for the uart). when the uart transmitter is enabled, this pin is confi gured as an output regardless of the value of ddrd1.  rxd ? port d, bit 0 rxd, receive data (data input pin for the uart). when the uart receiver is enabled this pin is configured as an input regardless of the value of ddrd0. when the uart forces this pin to be an input, a logical one in portd0 will turn on the internal pull-up. port d schematics note that all port pins are synchronized. the synchronization latches are not shown in the figures. figure 74. portd schematic diagram (pin pd0) da ta bus d d q q reset reset c c wd wp rd mos pull- up pd0 rxd rxen wp: wd: rl: rp: rd: rxd: rxen: write portd write ddrd read portd latch read portd pin read ddrd uart receive data uart receive enable ddd0 portd0 rl rp pud pud: pull-up disable
131 atmega163(l) 1142e?avr?02/03 figure 75. portd schematic diagram (pin pd1) figure 76. portd schematic diagram (pins pd2 and pd3) da ta bus d d q q reset reset c c wd wp rd rp rl mos pull- up pd1 r r wp: wd: rl: rp: rd: txd: txen: write portd write ddrd read portd latch read portd pin read ddrd uart transmit data uart transmit enable ddd1 portd1 txen txd pud pud: pull-up disable pud pud: pull-up disable n: m: 2, 3 0, 1
132 atmega163(l) 1142e?avr?02/03 figure 77. portd schematic diagram (pins pd4 and pd5) figure 78. portd schematic diagram (pin pd6) pud pud: pull-up disable data bus d d q q reset reset c c wd wp rd mos pull- up pd6 r r wp: wd: rl: rp: rd: acic: aco: write portd write ddrd read portd latch read portd pin read ddrd comparator ic enable comparator output ddd6 portd6 noise canceler edge select icf1 icnc1 ices1 0 1 acic aco rl rp pud pud: pull-up disable
133 atmega163(l) 1142e?avr?02/03 figure 79. portd schematic diagram (pin pd7) pud pud: pull-up disable
134 atmega163(l) 1142e?avr?02/03 memory programming boot loader support the atmega163 provides a mechanism for programming and re-programming code by the mcu itself. this feature allows flexible application software updates, controlled by the mcu using a flash-resident boot loader program. this makes it possible to pro- gram the avr in a target system without access to its spi pins. the boot loader program can use any available data interface and associated protocol, such as uart serial bus interface, to input or output program code, and write (program) that code into the flash memory, or read the code from the flash memory. the atmega163 flash memory is organized in two main sections:  the application flash section  the boot loader flash section the application flash section and the boot loader flash section have seperate boot lock bits. thus the user can select different levels of protection for the two sections. the store program memory (spm) instruction can only be executed from the boot loader flash section. the program flash memory in atmega163 is divided into 128 pages of 64 words each. the boot loader flash section is located at the high address space of the flash, and can be configured through the bootsz fuses as shown in table 51. table 51. boot size configuration bootsz1 bootsz0 boot size pages application flash addresses boot flash addresses 11 128 words 2 $0000 - $1f7f $1f80 - $1fff 10 256 words 4 $0000 - $1eff $1f00 - $1fff 01 512 words 8 $0000 - $1dff $1e00 - $1fff 00 1024 words 16 $0000 - $1bff $1c00 - $1fff
135 atmega163(l) 1142e?avr?02/03 figure 80. memory sections $0000 $1fff program memory bootsz = '11' application flash section (8064 x 16) boot flash section (128 x 16) $1f7f $1f80 $0000 $1fff program memory bootsz = '10' application flash section (7936 x 16) boot flash section (256 x 16) $1eff $1f00 $0000 $1fff program memory bootsz = '01' application flash section (7680 x 16) boot flash section (512 x 16) $1dff $1e00 $0000 $1fff program memory bootsz = '00' application flash section (7168 x 16) boot flash section (1024 x 16) $1bff $1c00 pages 126 2 pages 124 4 pages 120 8 pages 112 16
136 atmega163(l) 1142e?avr?02/03 entering the boot loader program the spm instruction can access the entire flash, but can only be executed from the boot loader flash section. if no boot loader capability is needed, the entire flash is available for application code. entering the boot loader takes place by a jump or call from the application program. this may be initiated by some trigger such as a command received via uart or spi interface, for example. alternatively, the boot reset fuse can be programmed so that the reset vector is pointing to the boot flash start address after a reset. in this case, the boot loader is started after a reset. after the application code is loaded, the program can start executing the application code. note that the fuses cannot be changed by the mcu itself. this means that once the boot reset fuse is pro- grammed, the reset vector will always point to the boot loader reset and the fuse can only be changed through the serial or parallel programming interface. capabilities of the boot loader the program code within the boot loader section has the capability to read from and write into the entire flash, including the boot loader memory. this allows the user to update both the application code and the boot loader code that handles the software update. the boot loader can thus even modify itself, and it can also erase itself from the code if the feature is not needed anymore. self-programming the flash programming of the flash is executed one page at a time. the flash page must be erased first for correct programming. the general write lock (lock bit 2) does not con- trol the programming of the flash memory by spm instruction. similarly, the general read/write lock (lock bit 1) does not control reading nor writing by lpm/spm, if it is attempted. the program memory can only be updated page by page, not word by word. one page is 128 bytes (64 words). the program memory will be modified by first performing page erase, then filling the temp orary page buffer one word at a time using spm, and then executing page write. if only part of the page needs to be changed, the other parts must be stored (for example in internal sram) before the erase, and then be re- written. the temporary page buffer can be accessed in a random sequence. it is essen- tial that the page address used in both the page erase and page write operation is addressing the same page. see ?assembly code example for a boot loader? on page 141 for an assembly code example. see table 60 on page 156 for typical programming times when using self- programming. performing page erase by spm to execute page erase, set up the address in the z-pointer, write ?00011? to the five lsb in spmcr and execute spm within four clock cycles after writing spmcr. the data in r1 and r0 is ignored. the page address must be written to z13:z7. other bits in the z-pointer will be ignored during this operation. it is recommended that the interrupts are disabled during the page erase operation. fill the temporary buffer (page load) to write an instruction word, set up the address in the z-pointer and data in r1:r0, write ?00001? to the five lsb in spmcr and execute spm within four clock cycles after writ- ing spmcr. the content of z6:z1 is used to address the data in the temporary buffer. z13:z7 must point to the page that is supposed to be written. table 52. boot reset fuse bootrst reset address 1 reset vector = application reset (address $0000) 0 reset vector = boot loader reset (see table 51)
137 atmega163(l) 1142e?avr?02/03 perform a page write to execute page write, set up the address in the z-pointer, write ?00101? to the five lsb in spmcr and execute spm within four clock cycles after writing spmcr. the data in r1 and r0 is ignored. the page address must be written to z13:z7. during this opera- tion, z6:z0 must be zero to ensure that the page is written correctly. it is recommended that the interrupts are disabled during the page write operation. consideration while updating the boot loader section special care must be taken if the user allows the boot loader section to be updated by leaving boot lock bit 11 unprogrammed. an accidental write to the boot loader itself can corrupt the entire boot loader, and further software updates might be impossible. if it is not necessary to change the boot loader software itself, it is recommended to pro- gram the boot lock bit 11 to protect the boot loader software from any internal software changes. wait for spm instruction to complete though the cpu is halted during page write, page erase or lock bit write, for future compatibility, the user software must poll for spm complete by reading the spmcr register and loop until the spmen bit is cleared after a programming operation. see ?assembly code example for a boot loader? on page 141 for a code example. instruction word read after page erase, page write, and lock bit write to ensure proper instruction pipelining after programming action (page erase, page write, or lock bit write), the spm instruction must be followed with the sequence (.dw $ffff - nop) as shown below: spm .dw $ffff nop if not, the instruction following spm might fail. it is not necessary to add this sequence when the spm instruction only loads the temporary buffer. avoid reading the application section during self- programming during self-programming (either page erase or page write), the user software should not read the application section. the user software itself must prevent addressing this section during the self-programming operations. this implies that interrupts must be disabled. before addressing the application section after the programming is completed, for future compatibility, the user software must write ?10001? to the five lsb in spmcr and execute spm within four clock cycles. then the user software should verify that the asb bit is cleared. see ?assembly code example for a boot loader? on page 141 for an example. though the asb and asre bits have no special function in this device, it is important for future code compatibility t hat they are treated as described above. boot loader lock bits atmega163 has two separate sets of boot lock bits which can be set independently. this gives the user a unique flexibility to select different levels of protection. the user can select:  to protect the entire flash from a software update by the mcu  to only protect the boot loader flash section from a software update by the mcu  to only protect application flash section from a software update by the mcu  allowing software update in the entire flash see table and table for further details. the boot lock bits can be set in software and in serial or parallel programming mode, but they can only be cleared by a chip erase command.
138 atmega163(l) 1142e?avr?02/03 note: 1. ?1? means unprogrammed, ?0? means programmed note: 1. ?1? means unprogrammed, ?0? means programmed setting the boot loader lock bits by spm to set the boot loader lock bits, write the desired data to r0, write ?00001001? to spmcr and execute spm within four clock cycles after writing spmcr. the only accessible lock bits are the boot lock bits that may prevent the application and boot loader section from any software update by the mcu. if bits 5..2 in r0 are cleared (zero), th e corresponding boot lock bit will be programmed if an spm instruction is executed within four cycles after blbset and spmen are set in spmcr. reading the fuse and lock bits from software it is possible to read both the fuse and lock bits from software. to read the lock bits, load the z-pointer with $0001 and set the blbset and spmen bits in spmcr. when an lpm instruction is executed within five cpu cycles after the blbset and spmen bits are set in spmcr, the value of the lock bits will be loaded in the destination regis- ter. the blbset and spmen bits will auto-clear upon completion of reading the lock bits or if no spm, or lpm, instruction is executed within four, respectively five, cpu cycles. when blbset and spmen are cleared, lpm will work as described in ?con- stant addressing using the lpm and spm instructions? on page 15 and in the instruction set manual. table 53. boot lock bit0 protection modes (application section) (1) blb0 mode blb02 blb01 protection 111 no restrictions for spm, lpm accessing the application section 2 1 0 spm is not allowed to write to the application section 300 spm is not allowed to write to the application section, and lpm executing from the boot loader section is not allowed to read from the application section 401 lpm executing from the boot loader section is not allowed to read from the application section table 54. boot lock bit1 protection modes (boot loader section) (1) blb1 mode blb12 blb11 protection 111 no restrictions for spm, lpm accessing the boot loader section 2 1 0 spm is not allowed to write to the boot loader section 300 spm is not allowed to write to the boot loader section, and lpm executing from the application section is not allowed to read from the boot loader section. if code is executed from boot section, the interrupts are disabled when blb12 is programmed. 401 lpm executing from the application section is not allowed to read from the boot loader section. if code is executed from boot section, the interrupts are disabled when blb12 is programmed. bit 76543210 r0 1 1 blb12 blb11 blb02 blb01 1 1
139 atmega163(l) 1142e?avr?02/03 the algorithm for reading the fuse low bits is similar to the one described above for reading the lock bits. to read the fuse low bits, load the z-pointer with $0000 and set the blbset and spmen bits in spmcr. when an lpm instruction is executed within five cycles after the blbset and spmen bits are set in the spmcr, the value of the fuse low bits will be loaded in the destination register as shown below. similarly, when reading the fuse high bits, load $0003 in the z-pointer. when an lpm instruction is executed within five cycles after the blbset and spmen bits are set in the spmcr, the value of the fuse high bits will be loaded in the destination register as shown below. fuse and lock bits that are programmed, will be read as zero. fuse and lock bits that are unprogrammed, will be read as one. in all cases, the read value of unused bit positions are undefined. eeprom write prevents writing to spmcr note that an eeprom write operation will block all software programming to flash. reading the fuses and lock bits from software will also be prevented during the eeprom write operation. it is recommended that the user checks the status bit (eewe) in the eecr register and verifies that the bit is cleared before writing to the spmcr register. if eeprom writing is performed inside an interrupt routine, the user software should disable that interrupt before checking the eewe status bit. addressing the flash during self-programming the z-pointer is used to address the spm commands. z15:z14 always ignored z13:z7 page select, for page erase and page write z6:z1 word select, for fillin g temp buffer (must be zero during page write operation) z0 should be zero for all spm commands, byte select for the lpm instruction. the only operation that does not use the z-pointer is setting the boot loader lock bits. the content of the z-pointer is ignored and will have no effect on the operation. note that the page erase and page write operation is addressed independently. there- fore it is of major importance that the boot loader software addresses the same page in both the page erase and page write operation. the lpm instruction also uses the z-pointer to store the address. since this instruction addresses the flash byte-by-byte, also the lsb (bit z0) of the z-pointer is used. see page 15 for a detailed description. bit 76543210 rd ? ? blb12 blb11 blb02 blb01 lb2 lb1 bit 7 6543210 rd bodlevel boden spien ? cksel3 cksel2 cksel1 cksel0 bit 7 6 5 4 3 2 1 0 rd ? ? ? ? ? bootsz1 bootsz0 bootrst bit 151413121110 9 8 zh (r31) z15 z14 z13 z12 z11 z10 z9 z8 zl (r30) z7z6z5z4z3z2z1z0 76543210
140 atmega163(l) 1142e?avr?02/03 store program memory control register ? spmcr the store program memory control register contains the control bits needed to control the programming of the flash from internal code execution.  bit 7 ? res: reserved bit this bit is a reserved bit in the atmega163 and always reads as zero. this bit should be written to zero when writing spmcr.  bit 6 ? asb: application section busy before entering the application section after a boot loader operation (page erase or page write) the user software must verify that this bit is cleared. in future devices, this bit will be set to ?1? by page erase and page write. in atmega163, this bit always reads as zero.  bit 5 ? res: reserved bit this bit is a reserved bit in the atmega163 and always reads as zero. this bit should be written to zero when writing spmcr.  bit 4 ? asre: application section read enable before re-entering the application section, the user software must set this bit together with the spmen bit and execute spm within four clock cycles.  bit 3 ? blbset: boot lock bit set if this bit is set at the same time as spmen, the next spm instruction within four clock cycles will set boot lock bits. alternatively, an lpm instruction within five cycles will read either the lock bbits or the fuse bits. the blbset bit will auto-clear upon comple- tion of the spm or lpm instruction, or if no spm, or lpm, instruction is executed within four, respectively five, clock cycles.  bit 2 ? pgwrt: page write if this bit is set at the same time as spmen, the next spm instruction within four clock cycles executes page write, with the data stored in the temporary buffer. the page address is taken from the high part of the z-pointer. the data in r1 and r0 are ignored. the pgwrt bit will auto-clear upon completion of a page write, or if no spm instruction is executed within four clock cycles. the cpu is halted during the entire page write operation.  bit 1 ? pgers: page erase if this bit is set at the same time as spmen, the next spm instruction within four clock cycles executes page erase. the page address is taken from the high part of the z- pointer. the data in r1 and r0 are ignored. the pgers bit will auto-clear upon comple- tion of a page erase, or if no spm instruction is executed within four clock cycles. the cpu is halted during the entire page erase operation. bit 7654 3 2 10 $37 ($57) ? asb ? asre blbset pgwrt pgers spmen spmcr read/write r r r r/w r/w r/w r/w r/w initial value x 0 0 0 0 0 0 0
141 atmega163(l) 1142e?avr?02/03  bit 0 ? spmen: store program memory enable this bit enables the spm instruction for the next four clock cycles. if set together with either asre, blbset, pgwrt, or pgers, the following spm instruction will have a special meaning, see description above. if only spmen is set, the following spm instruction will store the value in r1:r0 in the temporary page buffer addressed by the z-pointer. the lsb of the z-pointer is ignored. the spmen bit will auto-clear upon com- pletion of an spm instruction, or if no spm instruction is executed within four clock cycles. during page erase and page write, the spmen bit remains high until the oper- ation is completed. writing any other combination than ?10001?, ?01001?, ?00101?, or ?00001? in the lower five bits will have no effect. preventing flash corruption during periods of low v cc , the flash can be corrupted because the supply voltage is too low for the cpu and the flash to operate properly. these issues are the same as for board level systems using the flash, and the same design solutions should be applied. a flash corruption can be caused by two situations when the voltage is too low. first, a regular write sequence to the flash requires a minimum voltage to operate correctly. secondly, the cpu itself can execute instructions incorrectly, if the supply voltage for executing instructions is too low. flash corruption can easily be avoided by following these design recommendations (one is sufficient): 1. keep the avr r eset active (low) during periods of insufficient power s upply voltage. this can be done be enabling the internal brown-out detector (bod) if the operating voltage matches the detection level. if not, an external low v cc reset protection circuit can be used. if a reset occurs while a write operation is in progress, the write operation will be completed provided that the power supply voltage is sufficient. the total reset time must be longer thatn the flash write time. this can be achieved by holding the external reset, or by selecting a long reset time-out. 2. keep the avr core in power-down sleep mode during periods of low v cc . this will prevent the cpu from attempting to decode and execute instructions, effec- tively protecting the flash from unintentional writes. assembly code example for a boot loader ;- the routine writes one page of data from ram to flash ; the first data location in ram is pointed to by the y pointer (lowest address) ; the first data location in flash is pointed to by the z-pointer (lowest address) ;- error handling is not included ;- the routine must be placed inside the boot space ; only code inside boot loader ; section should be read during self-programming. ;- registers used: r0, r1, temp1, temp2, looplo, loophi, spmcrval ; storing and restoring of registers is not included in the routine ; register usage can be optimized at the expense of code size ;- it is assumed that the interrupts are disabled .equ pagesizeb = pagesize*2 ;pagesizeb is page size in bytes, not words .org smallbootstart write_page: ; page erase ldi spmcrval, (1< 142 atmega163(l) 1142e?avr?02/03 ; re-enable the application section ldi spmcrval, (1< 143 atmega163(l) 1142e?avr?02/03 sbrc temp1, spmen rjmp wait_spm ret program and data memory lock bits the atmega163 provides six lock bits which can be left unprogrammed (?1?) or can be programmed (?0?) to obtain the additional features listed in table 55. the lock bits can only be erased to ?1? with the chip erase command. note: 1. program the fuse bits before programming the lock bits. table 55. lock bit protection modes memory lock bits protection type lb mode lb1 lb2 111 no memory lock features enabled for parallel and serial programming. 201 further programming of the flash and eeprom is disabled in parallel and serial programming mode. the fuse bits are locked in both serial and parallel programming mode. (1) 300 further programming and verification of the flash and eeprom is disabled in parallel and serial programming mode. the fuse bits are locked in both serial and parallel programming mode. (1) blb0 mode blb01 blb02 111 no restrictions for spm, lpm accessing the application section. 2 0 1 spm is not allowed to write to the application section. 300 spm is not allowed to write to the application section, and lpm executing from the boot loader section is not allowed to read from the application section. 410 lpm executing from the boot loader section is not allowed to read from the application section. blb1 mode blb11 blb12 111 no restrictions for spm, lpm accessing the boot loader section. 2 0 1 spm is not allowed to write to the boot loader section. 300 spm is not allowed to write to the boot loader section, and lpm executing from the application section is not allowed to read from the boot loader section. if code executed from the boot section, the interrupts are disabled when blb12 is programmed. 410 lpm executing from the application section is not allowed to read from the boot loader section. if code executed from the boot section, the interrupts are disabled when blb12 is programmed.
144 atmega163(l) 1142e?avr?02/03 fuse bits the atmega163 has ten fuse bits, divided in two groups. the fuse high bits are bootsz1..0 and bootrst, and the fuse low bits are bodlevel, boden, spien, and cksel3..0.  bootsz1..0 select the size and start address of the boot flash section according to table 51 on page 134. default value is ?11? (both unprogrammed).  when bootrst is programmed (?0?), the reset vector is set to the start address of the boot flash section, as selected by the bootsz fuses according to table 51 on page 134. if the bootrst is unprogrammed (?1?), the reset vector is set to address $0000. default value is unprogrammed (?1?).  the bodlevel fuse selects the brown-out detection level and changes the start- up times, according to table 4 on page 24 and table 5 on page 25, respectively. default value is unprogrammed (?1?).  when the boden fuse is programmed (?0?), the brown-out detector is enabled. see ?reset and interrupt handling? on page 21. default value is unprogrammed (?1?).  when the spien fuse is programmed (?0?), serial program and data downloading are enabled. default value is programmed (?0?). the spien fuse is not accessible in serial programming mode.  cksel3..0 select the clock source and the start-up delay after reset, according to table 1 on page 5 and table 5 on page 25. default value is ?0010? (internal rc oscillator). the status of the fuse bits is not affected by chip erase. note that the fuse bits are locked if lock bit1 (lb1) is programmed. program the fuse bits before programming the lock bits. signature bytes all atmel microcontrollers have a three-byte signature code which identifies the device. this code can be read in both serial and parallel mode. the three bytes reside in a sep- arate address space. the atmega163 the signature bytes are: 1. $000: $1e (indicates manufactured by atmel). 2. $001: $94 (indicates 16kb flash memory). 3. $002: $02 (indicates atmega163 device when $001 is $94). calibration byte the atmega163 has a one byte calibration value for the internal rc oscillator. this byte resides in the high byte of address $000 in the signature address space. during memory programming, the external programmer must read this location and program it into a selected location in the normal flash program memory. at start-up, the user soft- ware must read this flash location and write the value to the osccal register.
145 atmega163(l) 1142e?avr?02/03 parallel programming this section describes how to parallel program and verify flash program memory, eeprom data memory + program and data memory lock bits and fuse bits in the atmega163. pulses are assumed to be at least 500ns unless otherwise noted. signal names in this section, some pins of the atmega163 are referenced by signal names describing their functionality during parallel programming, see figure 81 and table 56. pins not described in the following table are referenced by pin names. the xa1/xa0 pins determine the action executed when the xtal1 pin is given a posi- tive pulse. the bit coding are shown in table 57. when pulsing wr or oe , the command loaded determines the action executed. the command is a byte where the different bits are assigned functions as shown in table 58. the bs2 pin should be low unless otherwise noted. figure 81. parallel programming table 56. pin name mapping signal name in programming mode pin name i/o function rdy/bsy pd1 o 0: device is busy programming, 1: device is ready for new command oe pd2 i output enable (active low) wr pd3 i write pulse (active low) bs1 pd4 i byte select 1 (?0? selects low byte, ?1? selects high byte) xa0 pd5 i xtal action bit 0 xa1 pd6 i xtal action bit 1 atmega163 vcc +5v avcc +5v gnd xtal1 pd1 pd2 pd3 pd4 pd5 pd6 pb7 - pb0 data reset pd7 +12 v bs1 xa0 xa1 oe rdy/bsy pagel pa0 wr bs2 agnd
146 atmega163(l) 1142e?avr?02/03 enter programming mode the following algorithm puts the device in parallel programming mode: 1. apply 4.5 - 5.5v between v cc and gnd. 2. set reset and bs pins to ?0? and wait at least 100 ns. 3. apply 11.5 - 12.5v to reset . any activity on bs1 within 100 ns after +12v has been applied to reset , will cause the device to fail entering programming mode. chip erase the chip erase command will erase the flash and eeprom memories and the lock bits. the lock bits are not reset until the program memory has been completely erased. the fuse bits are not changed. a chip erase must be performed before the flash is re- programmed. load command ?chip erase? 1. set xa1, xa0 to ?10?. this enables command loading. 2. set bs1 to ?0?. pagel pd7 i program memory page load bs2 pa0 i byte select 2 (?0? selects low byte, ?1? selects 2?nd high byte) data pb7 - 0 i/o bidirectional databus (output when oe is low) table 57. xa1 and xa0 coding xa1 xa0 action when xtal1 is pulsed 00 load flash or eeprom address (high or low address byte determined by bs1) 0 1 load data (high or low data byte for flash determined by bs1) 1 0 load command 1 1 no action, idle table 58. command byte bit coding command byte command executed 1000 0000 chip erase 0100 0000 write fuse bits 0010 0000 write lock bits 0001 0000 write flash 0001 0001 write eeprom 0000 1000 read signature bytes 0000 0100 read fuse and lock bits 0000 0010 read flash 0000 0011 read eeprom table 56. pin name mapping (continued) signal name in programming mode pin name i/o function
147 atmega163(l) 1142e?avr?02/03 3. set data to ?1000 0000?. this is the command for chip erase. 4. give wr a negative pulse. this starts the chip erase. rdy/bsy goes low. 5. wait until rdy/bsy goes high before loading a new command. programming the flash the flash is organized as 128 pages of 128 bytes each. when programming the flash, the program data is latched into a page buffer. this allows one page of program data to be programmed simultaneously. the following procedure describes how to program the entire flash memory: a. load command ?write flash? 1. set xa1, xa0 to ?10?. this enables command loading. 2. set bs1 to ?0?. 3. set data to ?0001 0000?. this is the command for write flash. 4. give xtal1 a positive pulse. this loads the command. b. load address low byte 1. set xa1, xa0 to ?00?. this enables address loading. 2. set bs1 to ?0?. this selects low address. 3. set data = address low byte ($00 - $ff). 4. give xtal1 a positive pulse. this loads the address low byte. c. load data low byte 1. set xa1, xa0 to ?01?. this enables data loading. 2. set data = data low byte ($00 - $ff). 3. give xtal1 a positive pulse. this loads the data byte. d. latch data low byte 1. set bs1 to ?0?. this selects low data byte. 2. give pagel a positive pulse. this latches the data low byte. (see figure 82 for signal waveforms) e. load data high byte 1. set bs1 to ?1?. this selects high data byte. 2. set xa1, xa0 to ?01?. this enables data loading. 3. set data = data high byte ($00 - $ff). 4. give xtal1 a positive pulse. this loads the data byte. f. latch data high byte 1. set bs1 to ?1?. this selects high data byte. 2. give pagel a positive pulse. this latches the data high byte. g. repeat b through f 64 ti mes to fill the page buffer. to address a page in the flash, seven bits are needed (128 pages). the five most sig- nificant bits are read from address high byte as described in section ?h? below. the two least significant page address bits however, are the two most significant bits (bit7 and bit6) of the latest loaded address low byte as described in section ?b?.
148 atmega163(l) 1142e?avr?02/03 h. load address high byte 1. 1. set xa1, xa0 to ?00?. this enables address loading. 2. set bs1 to ?1?. this selects high address. 3. set data = address high byte ($00 - $1f). 4. give xtal1 a positive pulse. this loads the address high byte. i. program page 1. give wr a negative pulse. this starts programming of the entire page of data. rdy/bsy goes low. 2. wait until rdy/bsy goes high. (see figure 83 for signal waveforms) j. end page programming 1. set xa1, xa0 to ?10?. this enables command loading. 2. set data to ?0000 0000?. this is the command for no operation. 3. give xtal1 a positive pulse. this loads the command, and the internal write signals are reset. k. repeat a through j 128 times or until all data has been programmed. figure 82. programming the flash waveforms $10 addr. low addr. high data low data xa1 xa2 bs1 xtal1 d y/bsy r eset wr oe +12v bs2 p agel
149 atmega163(l) 1142e?avr?02/03 figure 83. programming the flash waveforms (continued) programming the eeprom the programming algorithm for the eeprom data memory is as follows (refer to ?pro- gramming the flash? on page 147 for details on command, address and data loading): 1. a: load command ?0001 0001?. 2. h: load address high byte ($00 - $01) 3. b: load address low byte ($00 - $ff) 4. e: load data low byte ($00 - $ff) l: write data low byte 1. set bs to ?0?. this selects low data. 2. give wr a negative pulse. this starts programming of the data byte. rdy/bsy goes low. 3. wait until to rdy/bsy goes high before programming the next byte. (see figure 84 for signal waveforms) the loaded command and address are retained in the device during programming. for efficient programming, the following should be considered.  the command needs only be loaded once when writing or reading multiple memory locations.  address high byte needs only be loaded before programming a new 256 word page in the eeprom.  skip writing the data value $ff, that is the contents of the entire eeprom after a chip erase. these considerations also applies to flash, eeprom and signature bytes reading. data high data xa1 xa0 bs1 xtal1 wr rdy/bsy reset +12v oe bs2 pagel
150 atmega163(l) 1142e?avr?02/03 figure 84. programming the eeprom waveforms reading the flash the algorithm for reading the flash memory is as follows (refer to ?programming the flash? on page 147 for details on command and address loading): 1. a: load command ?0000 0010?. 2. h: load address high byte ($00 - $1f). 3. b: load address low byte ($00 - $ff). 4. set oe to ?0?, and bs1 to ?0?. the flash word low byte can now be read at data. 5. set bs to ?1?. the flash word high byte can now be read at data. 6. set oe to ?1?. reading the eeprom the algorithm for reading the eeprom memory is as follows (refer to ?programming the flash? on page 147 for details on command and address loading): 1. a: load command ?0000 0011?. 2. h: load address high byte ($00 - $01). 3. b: load address ($00 - $ff). 4. set oe to ?0?, and bs1 to ?0?. the eeprom data byte can now be read at data. 5. set oe to ?1?. programming the fuse low bits the algorithm for programming the fuse low bits is as follows (refer to ?programming the flash? on page 147 for details on command and data loading): 1. a: load command ?0100 0000?. 2. c: load data low byte. bit n = ?0? programs and bit n = ?1? erases the fuse bit. bit 7 = bodlevel fuse bit bit 6 = boden fuse bit bit 5 = spien fuse bit bit 3..0 = cksel3..0 fuse bits bit 4 = ?1?. this bit is reserved and should be left unprogrammed (?1?). 3. give wr a negative pulse and wait for rdy/bsy to go high. $11 addr. high addr. low data low data xa1 xa2 bs1 xtal1 rdy/bsy reset wr oe +12v bs2 pagel
151 atmega163(l) 1142e?avr?02/03 programming the fuse high bits the algorithm for programming the fuse high bits is as follows (refer to ?programming the flash? on page 147 for details on command and data loading): 1. a: load command ?0100 0000?. 2. c: load data low byte. bit n = ?0? programs and bit n = ?1? erases the fuse bit. bit 2..1 = bootsz1..0 fuse bits bit 0 = bootrst fuse bit bit 7..3 = ?1?. these bits are reserved and should be left unprogrammed (?1?). 3. set bs1 to ?1?. this selects high data byte. 4. give wr a negative pulse and wait for rdy/bsy to go high. 5. set bs1 to ?0?. this selects low data byte. programming the lock bits the algorithm for programming the lock bits is as follows (refer to ?programming the flash? on page 147 for details on command and data loading): 1. a: load command ?0010 0000?. 2. d: load data low byte. bit n = ?0? programs the lock bit. bit 5 = boot lock bit12 bit 4 = boot lock bit11 bit 3 = boot lock bit02 bit 2 = boot lock bit01 bit 1 = lock bit2 bit 0 = lock bit1 bit 7..6 = ?1?. these bits are reserved and should be left unprogrammed (?1?). 3. l: write data low byte. the lock bits can only be cleared by executing chip erase. reading the fuse and lock bits the algorithm for reading the fuse and lock bits is as follows (refer to ?programming the flash? on page 147 for details on command loading): 1. a: load command ?0000 0100?. 2. set oe to ?0?, bs2 to ?0? and bs1 to ?0?. the status of the fuse low bits can now be read at data (?0? means programmed). bit 7 = bodlevel fuse bit bit 6 = boden fuse bit bit 5 = spien fuse bit bit 3..0 = cksel3..0 fuse bits 3. set oe to ?0?, bs2 to ?1? and bs1 to ?1?. the status of the fuse high bits can now be read at data (?0? means programmed). bit 2..1 = bootsz1..0 fuse bits bit 0 = bootrst fuse bit 4. set oe to ?0?, bs2 to ?0? and bs1 to ?1?. the status of the lock bits can now be read at data (?0? means programmed). bit 5 = boot lock bit12 bit 4 = boot lock bit11 bit 3 = boot lock bit02 bit 2 = boot lock bit01 bit 1 = lock bit2 bit 0 = lock bit1 5. set oe to ?1?.
152 atmega163(l) 1142e?avr?02/03 reading the signature bytes the algorithm for reading the signature bytes is as follows (refer to programming the flash for details on command and address loading): 1. a: load command ?0000 1000?. 2. c: load address low byte ($00 - $02). 3. set oe to ?0?, and bs to ?0?. the selected signature byte can now be read at data. 4. set oe to ?1?. reading the calibration byte the algorithm for reading the calibration byte is as follows (refer to programming the flash for details on command and address loading): 1. a: load command ?0000 1000?. 2. c: load address low byte, $00. set oe to ?0?, and bs1 to ?1?. the calibaration byte can now be read at data. 3. set oe to ?1?.
153 atmega163(l) 1142e?avr?02/03 parallel programming characteristics figure 85. parallel programming timing notes: 1. t wlrh is valid for the write eeprom, write fuse bits and write lock bits commands. 2. t wlrh_ce is valid for the chip erase command. 3. t wlrh_flash is valid for the write flash command. table 59. parallel programming characteristics, t a = 25 c 10%, v cc = 5 v 10% symbol parameter min typ max units v pp programming enable voltage 11.5 12.5 v i pp programming enable current 250 a t dvxh data and control valid before xtal1 high 67 ns t xhxl xtal1 pulse width high 67 ns t xldx data and control hold after xtal1 low 67 ns t xlwl xtal1 low to wr low 67 ns t bvph bs1 valid before pagel high 67 ns t phpl pagel pulse width high 67 ns t plbx bs1 hold after pagel low 67 ns t plwl pagel low to wr low 67 ns t bvwl bs1 valid to wr low 67 ns t rhbx bs1 hold after rdy/bsy high 67 ns t wlwh wr pulse width low 67 ns t wlrl wr low to rdy/bsy low 0 2.5 s t wlrh wr low to rdy/bsy high (1) 11.51.9ms t wlrh_ce wr low to rdy/bsy high for chip erase (2) 16 23 30 ms t wlrh_flash wr low to rdy/bsy high for write flash (3) 81215ms t xlol xtal1 low to oe low 67 ns t oldv oe low to data valid 20 ns t ohdz oe high to data tri-stated 20 ns data & contol (data, xa0/1, bs1, bs2) data write read xtal1 t xhxl t wlwh t dvxh t xlol t oldv t xldx t plwl t wlrh wr rdy/bsy oe pagel t phpl t plbx t bvph t xlwl t rhbx t ohdz t bvwl wlrl
154 atmega163(l) 1142e?avr?02/03 serial downloading both the flash and eeprom memory arrays can be programmed using the serial spi bus while reset is pulled to gnd. the serial interface consists of pins sck, mosi (input) and miso (output). after reset is set low, the programming enable instruction needs to be executed first before program/erase operations can be executed. figure 86. serial programming and verify (1) notes: 1. if the device is clocked by the internal oscillator, connecting a clock source to xtal1 is not required. 2. v cc - 0.3 v < avcc < v cc + 0.3 v, however, avcc should always be within 2.7 - 5.5 v. when programming the eeprom, an auto-erase cycle is built into the self-timed pro- gramming operation (in the serial mode only) and there is no need to first execute the chip erase instruction. the chip erase operation turns the content of every memory location in both the program and eeprom arrays into $ff. the program and eeprom memory arrays have separate address spaces: $0000 to $1fff for program memory and $0000 to $01ff for eeprom memory. the device can be clocked by any clock option during serial programming. the mini- mum low and high periods for the serial clock (sck) input are defined as follows: low: > 2 mcu clock cycles high: > 2 mcu clock cycles atmega163 vcc 2.7 - 5.5v 2.7 - 5.5v (2) gnd xtal1 pb6 pb7 pb5 sck miso mosi reset avcc agnd
155 atmega163(l) 1142e?avr?02/03 serial programming algorithm when writing serial data to the atmega163, data is clocked on the rising edge of sck. when reading data from the atmega163, data is clocked on the falling edge of sck. see figure 87, figure 88 and table 62 for timing details. to program and verify the atmega163 in the serial programming mode, the following sequence is recommended (see four byte instruction formats in table 61): 1. power-up sequence: apply power between v cc and gnd while r eset and sck are set to ?0?. in accor- dance with the setting of cksel fuses, apply a crystal/res onator, external clock, or rc network, or let the device run on the internal rc oscillator. in some systems, the programmer can not guarantee that sck is held low during power-up. in this case, wait for 100 ms after sck has been set to ?0?. reset must be then given a positive pulse of at least two xtal1 cycles duration and then set to ?0?. 2. wait for at least 20 ms and enable serial programming by sending the program- ming enable serial instruction to pin mosi/pb5. 3. the serial programming instructions will not work if the communication is out of synchronization. when in sync. the second byte ($53), will echo back when issu- ing the third byte of the programming enable instruction. whether the echo is correct or not, all four bytes of the instruction must be transmitted. if the $53 did not echo back, give sck a positive pulse and issue a new programming enable command. if the $53 is not seen within 32 attempts, there is no functional device connected. 4. if a chip erase is performed (must be done to erase the flash), wait 2t wd_flash after the instruction, give reset a positive pulse, and start over from step 2. see table 60 for the t wd_flash figure. 5. the flash is programmed one page at a time. the memory page is loaded one byte at a time by supplying the 6 lsb of the address and data together with the load program memory page instruction. the program memory page is stored by loading the write program memory page instruction with the 7 msb of the address. if polling is not used, the user must wait at least t wd_flash before issu- ing the next page. (please refer to table 60). accessing the serial programming interface before the flash write operation completes can result in incorrect programming. 6. the eeprom array is programmed one byte at a time by supplying the address and data together with the appropriate write instruction. an eeprom memory location is first automatically erased before new data is written. if polling is not used, the user must wait at least t wd_eeprom before issuing the next byte. (please refer to table 60). in a chip erased device, no $ffs in the data file(s) need to be programmed. 7. any memory location can be verified by using the read instruction which returns the content at the selected address at serial output miso/pb6. 8. at the end of the programming session, reset can be set high to commence normal operation. 9. power-off sequence (if needed): set xtal1 to ?0? (if external clock is used). set reset to ?1?. turn v cc power-off.
156 atmega163(l) 1142e?avr?02/03 data polling flash when a page is being programmed into the flash, reading an address location within the page being programmed will give the value $ff. at the time the device is ready for a new page, the programmed value w ill read correctly. this is used to determine when the next page can be written. note that the entire page is written simultaneously and any address within the page can be used for polling. data polling of the flash will not work for the value $ff, so when programming this value, the user will have to wait for at least t wd_flash before programming the next page. as a chip-erased device contains $ff in all locations, programming of addresses that are meant to contain $ff, can be skipped. see table 60 for t wd_flash value. data polling eeprom when a new byte has been written and is being programmed into eeprom, reading the address location being programmed will give the value $ff. at the time the device is ready for a new byte, the programmed value will read correctly. this is used to deter- mine when the next byte can be written. this will not work for the value $ff, but the user should have the following in mind: as a chip-erased device contains $ff in all locations, programming of addresses that are meant to contain $ff, can be skipped. this does not apply if the eeprom is re-programmed without chip-erasing the device. in this case, data polling cannot be used for the value $ff, and the user will have to wait at least t wd_eeprom before programming the next byte. see table 60 for t wd_eeprom value. programming times for non- volatile memory the internal rc oscillator is used to control programming time when programming or erasing flash, eeporm, fuses, and lock bits. during parallel or serial programming, the device is in reset, and this oscillator runs at its initial, uncalibrated frequency, which may vary from 0.5 mhz to 1.0 mhz. in software it is possible to calibrate this oscillator to 1.0 mhz (see ?calibrated internal rc oscillator? on page 37). consequently, program- ming times will be shorter and more accura te when programming or erasing non-volatile memory from software, using spm or the eeprom interface. see table 60 for a sum- mary of programming times. notes: 1. includes variation over voltage and temperature after rc oscillator has been cali- brated to 1.0 mhz 2. parallel eeprom programming takes 1k cycles 3. per page table 60. maximum programming times for non-volatile memory operation symbol number of rc oscillator cycles parallel/serial programming self- programming (1) 2.7v 5.0v chip erase t wd_ce 16k 32 ms 30 ms 17 ms flash write (3) t wd_flash 8k 16 ms 15 ms 8.5 ms eeprom write (2) t wd_eeprom 2k 4 ms 3.8 ms 2.2 ms fuse/lock bit write t wd_fuse 1k 2 ms 1.9 ms 1.1 ms
157 atmega163(l) 1142e?avr?02/03 figure 87. serial programming waveforms msb msb lsb lsb serial clock input pb7(sck) serial data input pb5 (mosi) pb6 (miso) sample serial data output
158 atmega163(l) 1142e?avr?02/03 . note: a = address high bits, b = address low bits, h = 0 - low byte, 1 - high byte, o = data out, i = data in, x = don?t care 1 = lock bit 1, 2 = lock bit 2, 3 = boot lock bit01, 4 = boot lock bit02, 5 = boot lock bit11, 6 = boot lock bit12, 7 = cksel0 fuse, 8 = cksel1 fuse, 9 = cksel2 fuse, a = cksel3 fuse, b = boden fuse, c = bodlevel fuse, d = bootrst fuse, e = bootsz0 fuse, f = bootsz1 fuse table 61. serial programming instruction set instruction instruction format operation byte 1 byte 2 byte 3 byte4 programming enable 1010 1100 0101 0011 xxxx xxxx xxxx xxxx enable serial programming after reset goes low. chip erase 1010 1100 100x xxxx xxxx xxxx xxxx xxxx chip erase eeprom and flash. read program memory 0010 h 000 xxxa aaaa bbbb bbbb oooo oooo read h (high or low) data o from program memory at word address a : b . load program memory page 0100 h 000 xxxx xxxx xx bb bbbb iiii iiii write h (high or low) data i to program memory page at word address b . write program memory page 0100 1100 xxxa aaaa bb xx xxxx xxxx xxxx write program memory page at address a : b . read eeprom memory 1010 0000 xxxx xxx a bbbb bbbb oooo oooo read data o from eeprom memory at address a : b . write eeprom memory 1100 0000 xxxx xxx a bbbb bbbb iiii iiii write data i to eeprom memory at address a : b . read lock bits 0101 1000 0000 0000 xxxx 0xxx xx 65 4321 read lock bits. ?0? = programmed, ?1? = unprogrammed. write lock bits 1010 1100 111x xxxx xxxx xxxx 11 65 4321 write lock bits. set bits 6 - 1 = ?0? to program lock bits. read signature byte 0011 0000 xxxx xxxx xxxx xx bb oooo oooo read signature byte o at address b . write fuse bits 1010 1100 1010 0000 xxxx xxxx cb 11 a987 set bits c - a , 9 - 7 = ?0? to program, ?1? to unprogram write fuse high bits 1010 1100 1010 1000 xxxx xxxx 1111 1 fed set bits f - d = ?0? to program, ?1? to unprogram read fuse bits 0101 0000 0000 0000 xxxx xxxx cb xx a987 read fuse bits. ?0? = programmed, ?1? = unprogrammed read fuse high bits 0101 1000 0000 1000 xxxx xxxx xxxx 1 fed read fuse high bits. ?0? = pro- grammed, ?1? = unprogrammed read calibration byte 0011 1000 xxxx xxxx 0000 0000 oooo oooo read signature byte o at address b .
159 atmega163(l) 1142e?avr?02/03 serial programming characteristics figure 88. serial programming timing table 62. serial programming characteristics, t a = -40 c to 85 c, v cc = 2.7v - 5.5v (unless otherwise noted) symbol parameter min typ max units 1/t clcl oscillator frequency (v cc = 2.7 - 5.5 v) 0 4 mhz t clcl oscillator period (v cc = 2.7 - 5.5 v) 250 ns 1/t clcl oscillator frequency (v cc = 4.0 - 5.5 v) 0 8 mhz t clcl oscillator period (v cc = 4.0 - 5.5 v) 125 ns t shsl sck pulse width high 2 t clcl ns t slsh sck pulse width low 2 t clcl ns t ovsh mosi setup to sck high t clcl ns t shox mosi hold after sck high 2 t clcl ns t sliv sck low to miso valid 10 16 32 ns mosi miso sck t ovsh t shsl t slsh t shox t sliv
160 atmega163(l) 1142e?avr?02/03 electrical characteristics absolute maximum ratings* operating temperature ................................. -55 c to +125 c *notice: stresses beyond those listed under ?absolute maximum ratings? may cause permanent dam- age to the device. this is a stress rating only and functional operation of the device at these or other conditions beyond those indicated in the operational sections of this specification is not implied. exposure to absolute maximum rating conditions for extended periods may affect device reliability. storage temperature .................................... -65c to +150c voltage on any pin except reset with respect to ground ................................-1.0v to v cc +0.5v voltage on reset with respect to ground .....-1.0v to +13.0v maximum operating voltage ............................................ 6.6v dc current per i/o pin ............................................... 40.0 ma dc current v cc and gnd pins................................ 200.0 ma dc characteristics t a = -40 c to 85 c, v cc = 2.7v to 5.5v (unless otherwise noted) symbol parameter condition min typ max units v il input low-voltage (except xtal1) -0.5 0.3 v cc (1) v v il1 input low-voltage (xtal1), cksel3 fuse programmed -0.5 0.3 v cc (1) v (xtal1), cksel3 fuse unprogrammed -0.5 0.2 v cc (1) v v ih input high-voltage (except xtal1, reset )0.6 v cc (2) v cc + 0.5 v v ih1 input high-voltage (xtal1), cksel3 fuse programmed 0.6 v cc (2) v cc + 0.5 v (xtal1), cksel3 fuse unprogrammed 0.8 v cc (2) v cc + 0.5 v v ih2 input high-voltage (reset )0.9 v cc (2) v cc + 0.5 v v ol output low-voltage (3) (ports a,b,c,d) i ol = 20 ma, v cc = 5v i ol = 10 ma, v cc = 3v 0.6 0.5 v v v oh output high-voltage (4) (ports a,b,c,d) i oh = -3 ma, v cc = 5v i oh = -1.5 ma, v cc = 3v 4.2 2.3 v v i il input leakage current i/o pin vcc = 5.5 v, pin low (absolute value) 8.0 a i ih input leakage current i/o pin vcc = 5.5 v, pin high (absolute value) 980 na rrst reset pull-up resistor 100 500 k ? r i/o i/o pin pull-up resistor 35 120 k ?
161 atmega163(l) 1142e?avr?02/03 notes: 1. ?max? means the highest value where the pin is guaranteed to be read as low 2. ?min? means the lowest value where the pin is guaranteed to be read as high 3. although each i/o port can sink more than the test conditions (20 ma at vcc = 5v, 10 ma at vcc = 3v) under steady state conditions (non-transient), the following must be observed: 1] the sum of all i ol , for all ports, should not exceed 200 ma. 2] the sum of all i ol , for ports b0 - b7, d0 - d7 and xtal2, should not exceed 100 ma. 3] the sum of all i ol , for ports a0 - a7 and c0 - c7 should not exceed 100 ma. if iol exceeds the test condition, v ol may exceed the related specification. pins are not guaranteed to sink current greater than the listed test condition. 4. although each i/o port can source more than the test conditions (3 ma at vcc = 5v, 1.5 ma at vcc = 3v) under steady state conditions (non-transient), the following must be observed: 1] the sum of all i oh , for all ports, should not exceed 200 ma. 2] the sum of all i oh , for ports b0 - b7, d0 - d7 and xtal2, should not exceed 100 ma. 3] the sum of all i oh , for ports a0 - a7 and c0 - c7 should not exceed 100 ma. if ioh exceeds the test condition, v oh may exceed the related specification. pins are not guaranteed to source current greater than the listed test condition. 5. minimum v cc for power-down is 2.5v. external clock drive waveforms figure 89. external clock drive waveforms i cc power supply current active 4 mhz, v cc = 3v (atmega163l) 5.0 ma active 8 mhz, v cc = 5v (atmega163) 15.0 ma idle 4 mhz, v cc = 3v (atmega163l) 2.5 ma idle 8 mhz, v cc = 5v (atmega163) 8ma power-down mode (5) wdt enabled, v cc = 3v 9 15.0 a wdt disabled, v cc = 3v <1 4.0 a v acio analog comparator input offset voltage v cc = 5v v in = v cc /2 40 mv i aclk analog comparator input leakage current v cc = 5v v in = v cc /2 -50 50 na t acid analog comparator initialization delay v cc = 2.7v v cc = 4.0v 750 500 ns dc characteristics (continued) t a = -40 c to 85 c, v cc = 2.7v to 5.5v (unless otherwise noted) symbol parameter condition min typ max units
162 atmega163(l) 1142e?avr?02/03 external clock drive note: r should be in the range 3k ? - 100k ? , and c should be at least 20pf. the c values given in the table includes pin capacitance. this will vary with package type. table 63. external clock drive symbol parameter v cc = 2.7v to 5.5 vv cc = 4.0v to 5.5 v units min max min max 1/t clcl oscillator frequency 0 4 0 8 mhz t clcl clock period 250 125 ns t chcx high time 100 50 ns t clcx low time 100 50 ns t clch rise time 1.6 0.5 s t chcl fall time 1.6 0.5 s table 64. external rc oscillator, typical frequencies r [k ? ]c [pf] f 100 70 100 khz 31.5 20 1.0 mhz 6.5 20 4.0 mhz
163 atmega163(l) 1142e?avr?02/03 two-wire serial interface characteristics table 65 describes the requirements for devices connected to the two-wire serial bus. the atmega163 two-wire serial interface meets or exceeds these requirements under the noted conditions. timing symbols refer to figure 90. notes: 1. in atmegan163, this parameter is characterized and not 100% tested. 2. required only for f scl > 100 khz. 3. c b = capacitance of one bus line in pf. 4. f ck = cpu clock frequency 5. this requirement applies to all atmega163 two-wire serial interface operation. other devices connected to the two-wire serial bus need only obey the general f scl requirement. 6. the actual low period generated by the atmega163 two-wire serial interface is (1/f scl - 2/f ck ), thus f ck must be greater than 6 mhz for the low time requirement to be strictly met at f scl = 100 khz. table 65. two-wire serial bus requirements symbol parameter condition min max units v il input low-voltage -0.5 0.3 v cc v v ih input high-voltage 0.7 v cc v cc + 0.5 v v hys (1) hysteresis of schmitt trigger inputs 0.05 v cc (2) ?v v ol (1) output low-voltage 3 ma sink current 0 0.4 v t of (1) output fall time from v ihmin to v ilmax 10 pf < c b < 400 pf (3) 20 + 0.1c b (3)(2) 250 ns t sp (1) spikes suppressed by input filter 0 50 (2) ns i i input current each i/o pin 0.1v cc < v i < 0.9v cc -10 10 a c i (1) capacitance for each i/o pin ? 10 pf f scl scl clock frequency f ck (4) > max(16f scl , 250khz) (5) 0217khz t hd;sta hold time (repeated) start condition f scl 100 khz 4.0 ? s f scl > 100 khz 0.6 ? s t low low period of the scl clock f scl 100 khz (6) 4.7 ? s f scl > 100 khz 1.3 ? s t high high period of the scl clock f scl 100 khz 4.0 ? s f scl > 100 khz 0.6 ? s t su;sta set?up time for a repeated start condition f scl 100 khz 4.7 ? s f scl > 100 khz 0.6 ? s t hd;dat data hold time f scl 100 khz 0 3.45 s f scl > 100 khz 0 0.9 s t su;dat data setup time f scl 100 khz 250 ? ns f scl > 100 khz 100 ? ns t su;sto setup time for stop condition f scl 100 khz 4.0 ? s f scl > 100 khz 0.6 ? s t buf bus free time between a stop and start condition f scl 100 khz 4.7 ? s f scl > 100 khz 1.3 ? s
164 atmega163(l) 1142e?avr?02/03 figure 90. two-wire serial bus timing t su;sta t low t high t low t of t hd;sta t hd;dat t su;dat t su;sto t buf scl sda
165 atmega163(l) 1142e?avr?02/03 typical characteristics the following charts show typical behavior. these figures are not tested during manu- facturing. all current consumption measurements are performed with all i/o pins configured as inputs and with internal pull-ups enabled. all pins on port f are pulled high externally. a sine wave generator with rail-to-rail output is used as clock source. the power consumption in power-down mode is independent of clock selection. the current consumption is a function of several factors such as: operating voltage, operating frequency, loading of i/o pins, switching rate of i/o pins, code executed and ambient temperature. the dominating factors are operating voltage and frequency. the current drawn from capacitive loaded pins may be estimated (for one pin) as c l  v cc  f, where c l = load capacitance, v cc = operating voltage and f = average switching frequency of i/o pin. the parts are characterized at frequencies higher than test limits. parts are not guaran- teed to function properly at frequencies higher than the ordering code indicates. the difference between current consumption in power-down mode with watchdog timer enabled and power-down mode with watchdog timer disabled represents the dif- ferential current drawn by the watchdog timer. figure 91. analog comparator offset voltage vs, common mode voltage (v cc = 5v) 0 2 4 6 8 10 12 14 16 18 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 common mode voltage (v) offset voltage (mv) t = 85 ? c a t = 25 ? c a
166 atmega163(l) 1142e?avr?02/03 figure 92. analog comparator offset voltage vs. common mode voltage (v cc = 2.7v) figure 93. analog comparator input leakage current (v cc = 6v; ta = 25 c) 0 2 4 6 8 10 0 0.5 1 1.5 2 2.5 3 common mode voltage (v) offset voltage (mv) t = 85 ? c a t = 25 ? c a 60 50 40 30 20 10 0 -10 0 0.5 1.5 122.53.5 3 4 4.5 5 6 6.5 7 5.5 i (na) aclk v (v) in
167 atmega163(l) 1142e?avr?02/03 figure 94. watchdog oscillator frequency vs. v cc sink and source capabilities of i/o ports are measured on one pin at a time. figure 95. pull-up resistor current vs. input voltage (v cc = 5v) 0 200 400 600 800 1000 1200 1400 1600 2 2.5 3 3.5 4 4.5 5 5.5 6 t = 85 ? c a t = 25 ? c a v (v) cc f (khz) rc 0 20 40 60 80 100 120 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 i ( a) op v (v) op t = 85 ? c a t = 25 ? c a
168 atmega163(l) 1142e?avr?02/03 figure 96. pull-up resistor current vs. input voltage (v cc = 2.7v) figure 97. i/o pin sink current vs. output voltage (v cc = 5v) 0 5 10 15 20 25 30 0 0.5 1 1.5 2 2.5 3 i ( a) op v (v) op t = 85 ? c a t = 25 ? c a 0 10 20 30 40 50 60 70 0 0.5 1 1.5 2 2.5 3 i (ma) ol v (v) ol t = 85 ? c a t = 25 ? c a
169 atmega163(l) 1142e?avr?02/03 figure 98. i/o pin source current vs. output voltage (v cc = 5v) figure 99. i/o pin sink current vs. output voltage (v cc = 2.7v) 0 2 4 6 8 10 12 14 16 18 20 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 i (ma) oh v (v) oh t = 85 ? c a t = 25 ? c a 0 5 10 15 20 25 0 0.5 1 1.5 2 i (ma) ol v (v) ol t = 85 ? c a t = 25 ? c a
170 atmega163(l) 1142e?avr?02/03 figure 100. i/o pin source current vs. output voltage (v cc = 2.7v) figure 101. i/o pin input threshold vs. v cc (t a = 25 c) 0 1 2 3 4 5 6 0 0.5 1 1.5 2 2.5 3 i (ma) oh v (v) oh t = 85 ? c a t = 25 ? c a 0 0.5 1 1.5 2 2.5 2.7 4.0 5.0 threshold voltage (v) v cc
171 atmega163(l) 1142e?avr?02/03 figure 102. i/o pin input hysteresis vs. v cc (t a = 25 c) 0 0.02 0.04 0.06 0.08 0.1 0.12 0.14 0.16 0.18 2.7 4.0 5.0 input hysteresis (v) v cc
172 atmega163(l) 1142e?avr?02/03 register summary address name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 page $3f ($5f) sreg i t h s v n z c 20 $3e ($5e) sph ? ? ? ? ? sp10 sp9 sp8 21 $3d ($5d) spl sp7 sp6 sp5 sp4 sp3 sp2 sp1 sp0 21 $3c ($5c) reserved $3b ($5b) gimsk int1 int0 ? ? ? ? ? ?30 $3a ($5a) gifr intf1 intf0 ? ? ? ? ? ?31 $39 ($59) timsk ocie2 toie2 ticie1 ocie1a ocie1b toie1 ?toie0 32 $38 ($58) tifr ocf2 tov2 icf1 ocf1a ocf1b tov1 ?tov0 32 $37 ($57) spmcr ?asb ? asre blbset pgwrt pgers spmen 140 $36 ($56) twcr twint twea twsta twsto twwc twen ?twie 82 $35 ($55) mcucr ? se sm1 sm0 isc11 isc10 isc01 isc00 34 $34 ($54) mcusr ? ? ? ? wdrfborfextrf porf 28 $33 ($53) tccr0 ? ? ? ? ? cs02 cs01 cs00 41 $32 ($52) tcnt0 timer/counter0 (8 bits) 42 $31 ($51) osccal oscillator calibration register 37 $30 ($50) sfior ? ? ? ? acme pud psr2 psr10 40 $2f ($4f) tccr1a com1a1 com1a0 com1b1 com1b0 foc1a foc1b pwm11 pwm10 44 $2e ($4e) tccr1b icnc1 ices1 ? ? ctc1 cs12 cs11 cs10 45 $2d ($4d) tcnt1h timer/counter1 ? counter register high byte 46 $2c ($4c) tcnt1l timer/counter1 ? counter register low byte 46 $2b ($4b) ocr1ah timer/counter1 ? output compare register a high byte 47 $2a ($4a) ocr1al timer/counter1 ? output compare register a low byte 47 $29 ($49) ocr1bh timer/counter1 ? output compare register b high byte 47 $28 ($48) ocr1bl timer/counter1 ? output compare register b low byte 47 $27 ($47) icr1h timer/counter1 ? input capture register high byte 48 $26 ($46) icr1l timer/counter1 ? input capture register low byte 48 $25 ($45) tccr2 foc2 pwm2 com21 com20 ctc2 cs22 cs21 cs20 52 $24 ($44) tcnt2 timer/counter2 (8 bits) 53 $23 ($43) ocr2 timer/counter2 output compare register 54 $22 ($42) assr ? ? ? ? as2 tcn2ub ocr2ub tcr2ub 57 $21 ($41) wdtcr ? ? ? wdtoe wde wdp2 wdp1 wdp0 60 $20 ($40) ubrrhi ? ? ? ? ubrr[11:8] 78 $1f ($3f) eearh ? ? ? ? ? ? ?eear8 62 $1e ($3e) eearl eear7 eear6 eear5 eear4 eear3 eear2 eear1 eear0 62 $1d ($3d) eedr eeprom data register 62 $1c ($3c) eecr ? ? ? ? eerie eemwe eewe eere 63 $1b ($3b) porta porta7 porta6 porta5 porta4 porta3 porta2 porta1 porta0 115 $1a ($3a) ddra dda7 dda6 dda5 dda4 dda3 dda2 dda1 dda0 115 $19 ($39) pina pina7 pina6 pina5 pina4 pina3 pina2 pina1 pina0 115 $18 ($38) portb portb7 portb6 portb5 portb4 portb3 portb2 portb1 portb0 117 $17 ($37) ddrb ddb7 ddb6 ddb5 ddb4 ddb3 ddb2 ddb1 ddb0 117 $16 ($36) pinb pinb7 pinb6 pinb5 pinb4 pinb3 pinb2 pinb1 pinb0 117 $15 ($35) portc portc7 portc6 portc5 portc4 portc3 portc2 portc1 portc0 123 $14 ($34) ddrc ddc7 ddc6 ddc5 ddc4 ddc3 ddc2 ddc1 ddc0 123 $13 ($33) pinc pinc7 pinc6 pinc5 pinc4 pinc3 pinc2 pinc1 pinc0 123 $12 ($32) portd portd7 portd6 portd5 portd4 portd3 portd2 portd1 portd0 128 $11 ($31) ddrd ddd7 ddd6 ddd5 ddd4 ddd3 ddd2 ddd1 ddd0 128 $10 ($30) pind pind7 pind6 pind5 pind4 pind3 pind2 pind1 pind0 128 $0f ($2f) spdr spi data register 69 $0e ($2e) spsr spif wcol ? ? ? ? ? spi2x 68 $0d ($2d) spcr spie spe dord mstr cpol cpha spr1 spr0 67 $0c ($2c) udr uart i/o data register 74 $0b ($2b) ucsra rxc txc udre fe or ?u2xmpcm 74 $0a ($2a) ucsrb rxcie txcie udrie rxen txen chr9 rxb8 txb8 76 $09 ($29) ubrr uart baud rate register 78 $08 ($28) acsr acd acbg aco aci acie acic acis1 acis0 102 $07 ($27) admux refs1 refs0 adlar mux4 mux3 mux2 mux1 mux0 110 $06 ($26) adcsr aden adsc adfr adif adie adps2 adps1 adps0 111 $05 ($25) adch adc data register high byte 112 $04 ($24) adcl adc data register low byte 112 $03 ($23) twdr two-wire serial interface data register 84 $02 ($22) twar twa6 twa5 twa4 twa3 twa2 twa1 twa0 twgce 85 $01 ($21) twsr tws7 tws6 tws5 tws4 tws3 ? ? ?84
173 atmega163(l) 1142e?avr?02/03 note: 1. for compatibility with future devices, reserved bits should be written to zero if accessed. reserved i/o memory addresse s should never be written. 2. some of the status flags are cleared by writing a logical one to them. note that the cbi and sbi instructions will operate on all bits in the i/o register, writing a one back into any flag read as set, thus clearing the flag. the cbi and sbi instruction s work with registers $00 to $1f only. $00 ($20) twbr two-wire serial interface bit rate register 82 register summary (continued) address name bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0 page
174 atmega163(l) 1142e?avr?02/03 instruction set summary mnemonics operands description operation flags #clocks arithmetic and logic instructions add rd, rr add two registers rd rd + rr z,c,n,v,h 1 adc rd, rr add with carry two registers rd rd + rr + c z,c,n,v,h 1 adiw rdl,k add immediate to word rdh:rdl rdh:rdl + k z,c,n,v,s 2 sub rd, rr subtract two registers rd rd - rr z,c,n,v,h 1 subi rd, k subtract constant from register rd rd - k z,c,n,v,h 1 sbc rd, rr subtract with carry two registers rd rd - rr - c z,c,n,v,h 1 sbci rd, k subtract with carry constant from reg. rd rd - k - c z,c,n,v,h 1 sbiw rdl,k subtract immediate from word rdh:rdl rdh:rdl - k z,c,n,v,s 2 and rd, rr logical and registers rd rd ? rr z,n,v 1 andi rd, k logical and register and constant rd rd ? k z,n,v 1 or rd, rr logical or registers rd rd v rr z,n,v 1 ori rd, k logical or register and constant rd rd v k z,n,v 1 eor rd, rr exclusive or registers rd rd rr z,n,v 1 com rd one?s complement rd $ff ? rd z,c,n,v 1 neg rd two?s complement rd $00 ? rd z,c,n,v,h 1 sbr rd,k set bit(s) in register rd rd v k z,n,v 1 cbr rd,k clear bit(s) in register rd rd ? ($ff - k) z,n,v 1 inc rd increment rd rd + 1 z,n,v 1 dec rd decrement rd rd ? 1 z,n,v 1 tst rd test for zero or minus rd rd ? rd z,n,v 1 clr rd clear register rd rd rd z,n,v 1 ser rd set register rd $ff none 1 mul rd, rr multiply unsigned r1:r0 rd x rr z,c 2 muls rd, rr multiply signed r1:r0 rd x rr z,c 2 mulsu rd, rr multiply signed with unsigned r1:r0 rd x rr z,c 2 fmul rd, rr fractional multiply unsigned r1:r0 (rd x rr) << 1 z,c 2 fmuls rd, rr fractional multiply signed r1:r0 (rd x rr) << 1 z,c 2 fmulsu rd, rr fractional multiply signed with unsigned r1:r0 (rd x rr) << 1 z,c 2 branch instructions rjmp k relative jump pc pc + k + 1 none 2 ijmp indirect jump to (z) pc z none 2 jmp k direct jump pc knone3 rcall k relative subroutine call pc pc + k + 1 none 3 icall indirect call to (z) pc znone3 call k direct subroutine call pc knone4 ret subroutine return pc stack none 4 reti interrupt return pc stack i 4 cpse rd,rr compare, skip if equal if (rd = rr) pc pc + 2 or 3 none 1 / 2 / 3 cp rd,rr compare rd ? rr z, n,v,c,h 1 cpc rd,rr compare with carry rd ? rr ? c z, n,v,c,h 1 cpi rd,k compare register with immediate rd ? k z, n,v,c,h 1 sbrc rr, b skip if bit in register cleared if (rr(b)=0) pc pc + 2 or 3 none 1 / 2 / 3 sbrs rr, b skip if bit in register is set if (rr(b)=1) pc pc + 2 or 3 none 1 / 2 / 3 sbic p, b skip if bit in i/o register cleared if (p(b)=0) pc pc + 2 or 3 none 1 / 2 / 3 sbis p, b skip if bit in i/o register is set if (p(b)=1) pc pc + 2 or 3 none 1 / 2 / 3 brbs s, k branch if status flag set if (sreg(s) = 1) then pc pc+k + 1 none 1 / 2 brbc s, k branch if status flag cleared if (sreg(s) = 0) then pc pc+k + 1 none 1 / 2 breq k branch if equal if (z = 1) then pc pc + k + 1 none 1 / 2 brne k branch if not equal if (z = 0) then pc pc + k + 1 none 1 / 2 brcs k branch if carry set if (c = 1) then pc pc + k + 1 none 1 / 2 brcc k branch if carry cleared if (c = 0) then pc pc + k + 1 none 1 / 2 brsh k branch if same or higher if (c = 0) then pc pc + k + 1 none 1 / 2 brlo k branch if lower if (c = 1) then pc pc + k + 1 none 1 / 2 brmi k branch if minus if (n = 1) then pc pc + k + 1 none 1 / 2 brpl k branch if plus if (n = 0) then pc pc + k + 1 none 1 / 2 brge k branch if greater or equal, signed if (n v= 0) then pc pc + k + 1 none 1 / 2 brlt k branch if less than zero, signed if (n v= 1) then pc pc + k + 1 none 1 / 2 brhs k branch if half carry flag set if (h = 1) then pc pc + k + 1 none 1 / 2 brhc k branch if half carry flag cleared if (h = 0) then pc pc + k + 1 none 1 / 2 brts k branch if t flag set if (t = 1) then pc pc + k + 1 none 1 / 2 brtc k branch if t flag cleared if (t = 0) then pc pc + k + 1 none 1 / 2 brvs k branch if overflow flag is set if (v = 1) then pc pc + k + 1 none 1 / 2 brvc k branch if overflow flag is cleared if (v = 0) then pc pc + k + 1 none 1 / 2
175 atmega163(l) 1142e?avr?02/03 brie k branch if interrupt enabled if ( i = 1) then pc pc + k + 1 none 1 / 2 brid k branch if interrupt disabled if ( i = 0) then pc pc + k + 1 none 1 / 2 data transfer instructions mov rd, rr move between registers rd rr none 1 movw rd, rr copy register word rd+1:rd rr+1:rr none 1 ldi rd, k load immediate rd knone1 ld rd, x load indirect rd (x) none 2 ld rd, x+ load indirect and post-inc. rd (x), x x + 1 none 2 ld rd, - x load indirect and pre-dec. x x - 1, rd (x) none 2 ld rd, y load indirect rd (y) none 2 ld rd, y+ load indirect and post-inc. rd (y), y y + 1 none 2 ld rd, - y load indirect and pre-dec. y y - 1, rd (y) none 2 ldd rd,y+q load indirect with displacement rd (y + q) none 2 ld rd, z load indirect rd (z) none 2 ld rd, z+ load indirect and post-inc. rd (z), z z+1 none 2 ld rd, -z load indirect and pre-dec. z z - 1, rd (z) none 2 ldd rd, z+q load indirect with displacement rd (z + q) none 2 lds rd, k load direct from sram rd (k) none 2 st x, rr store indirect (x) rr none 2 st x+, rr store indirect and post-inc. (x) rr, x x + 1 none 2 st - x, rr store indirect and pre-dec. x x - 1, (x) rr none 2 st y, rr store indirect (y) rr none 2 st y+, rr store indirect and post-inc. (y) rr, y y + 1 none 2 st - y, rr store indirect and pre-dec. y y - 1, (y) rr none 2 std y+q,rr store indirect with displacement (y + q) rr none 2 st z, rr store indirect (z) rr none 2 st z+, rr store indirect and post-inc. (z) rr, z z + 1 none 2 st -z, rr store indirect and pre-dec. z z - 1, (z) rr none 2 std z+q,rr store indirect with displacement (z + q) rr none 2 sts k, rr store direct to sram (k) rr none 2 lpm load program memory r0 (z) none 3 lpm rd, z load program memory rd (z) none 3 lpm rd, z+ load program memory and post-inc rd (z), z z+1 none 3 spm store program memory (z) r1:r0 none - in rd, p in port rd pnone1 out p, rr out port p rr none 1 push rr push register on stack stack rr none 2 pop rd pop register from stack rd stack none 2 bit and bit-test instructions sbi p,b set bit in i/o register i/o(p,b) 1none2 cbi p,b clear bit in i/o register i/o(p,b) 0none2 lsl rd logical shift left rd(n+1) rd(n), rd(0) 0 z,c,n,v 1 lsr rd logical shift right rd(n) rd(n+1), rd(7) 0 z,c,n,v 1 rol rd rotate left through carry rd(0) c,rd(n+1) rd(n),c rd(7) z,c,n,v 1 ror rd rotate right through carry rd(7) c,rd(n) rd(n+1),c rd(0) z,c,n,v 1 asr rd arithmetic shift right rd(n) rd(n+1), n=0..6 z,c,n,v 1 swap rd swap nibbles rd(3..0) rd(7..4),rd(7..4) rd(3..0) none 1 bset s flag set sreg(s) 1sreg(s)1 bclr s flag clear sreg(s) 0 sreg(s) 1 bst rr, b bit store from register to t t rr(b) t 1 bld rd, b bit load from t to register rd(b) tnone1 sec set carry c 1c1 clc clear carry c 0 c 1 sen set negative flag n 1n1 cln clear negative flag n 0 n 1 sez set zero flag z 1z1 clz clear zero flag z 0 z 1 sei global interrupt enable i 1i1 cli global interrupt disable i 0 i 1 ses set signed test flag s 1s1 cls clear signed test flag s 0 s 1 sev set twos complement overflow. v 1v1 clv clear twos complement overflow v 0 v 1 set set t in sreg t 1t1 clt clear t in sreg t 0 t 1 seh set half carry flag in sreg h 1h1 instruction set summary (continued)
176 atmega163(l) 1142e?avr?02/03 clh clear half carry flag in sreg h 0 h 1 nop no operation none 1 sleep sleep (see specific descr. for sleep function) none 1 wdr watchdog reset (see specific descr. for wdr/timer) none 1 instruction set summary (continued)
177 atmega163(l) 1142e?avr?02/03 ordering information speed (mhz) power supply ordering code package operation range 4 2.7 - 5.5v ATMEGA163L-4AC atmega163l-4pc 44a 40p6 commercial (0 c to 70 c) atmega163l-4ai atmega163l-4pi 44a 40p6 industrial (-40 c to 85 c) 8 4.0 - 5.5v atmega163-8ac atmega163-8pc 44a 40p6 commercial (0 c to 70 c) atmega163-8ai atmega163-8pi 44a 40p6 industrial (-40 c to 85 c) package type 44a 44-lead, thin (1.0 mm) plastic gull wing quad flat package (tqfp) 40p6 40-lead, 0.600" wide, plastic dual inline package (pdip)
178 atmega163(l) 1142e?avr?02/03 packaging information 44a 2325 orchard parkway san jose, ca 95131 title drawing no. r rev. 44a, 44-lead, 10 x 10 mm body size, 1.0 mm body thickness, 0.8 mm lead pitch, thin profile plastic quad flat package (tqfp) b 44a 10/5/2001 pin 1 identifier 0?~7? pin 1 l c a1 a2 a d1 d e e1 e b common dimensions (unit of measure = mm) symbol min nom max note notes: 1. this package conforms to jedec reference ms-026, variation acb. 2. dimensions d1 and e1 do not include mold protrusion. allowable protrusion is 0.25 mm per side. dimensions d1 and e1 are maximum plastic body size dimensions including mold mismatch. 3. lead coplanarity is 0.10 mm maximum. a ? ? 1.20 a1 0.05 ? 0.15 a2 0.95 1.00 1.05 d 11.75 12.00 12.25 d1 9.90 10.00 10.10 note 2 e 11.75 12.00 12.25 e1 9.90 10.00 10.10 note 2 b 0.30 ? 0.45 c 0.09 ? 0.20 l 0.45 ? 0.75 e 0.80 typ
179 atmega163(l) 1142e?avr?02/03 40p6 2325 orchard parkway san jose, ca 95131 title drawing no. r rev. 40p6 , 40-lead (0.600"/15.24 mm wide) plastic dual inline package (pdip) b 40p6 09/28/01 pin 1 e1 a1 b ref e b1 c l seating plane a 0o ~ 15o d e eb common dimensions (unit of measure = mm) symbol min nom max note a ? ? 4.826 a1 0.381 ? ? d 52.070 ? 52.578 note 2 e 15.240 ? 15.875 e1 13.462 ? 13.970 note 2 b 0.356 ? 0.559 b1 1.041 ? 1.651 l 3.048 ? 3.556 c 0.203 ? 0.381 eb 15.494 ? 17.526 e 2.540 typ notes: 1. this package conforms to jedec reference ms-011, variation ac. 2. dimensions d and e1 do not include mold flash or protrusion. mold flash or protrusion shall not exceed 0.25 mm (0.010").
180 atmega163(l) 1142e?avr?02/03 erratas atmega163(l) errata rev. f  increased interrupt latency  interrupts abort twi power-down  twi master does not accept spikes on bus lines  twcr write operations ignored  pwm not phase correct  twi is speed limited in slave mode 6. increased interrupt latency in this device, some instructions are not interruptable, and will cause the interrupt latency to increase. the only practical problem concerns a loop followed by a two- word instruction while waiting for an interrupt. the loop may consist of a branch instruction or an absolute or relative jump back to itself like this: loop: rjmp loop in this case, a dead-lock situation arises. problem fix/workaround in assembly, insert a nop instruction immediately after a loop to itself. the problem will normally be detected during development. in c, the only construct that will give this problem is an empty ?for? loop; ?for(;;)?. use ?while(1)? or ?do{} while (1)? to avoid the problem. 5. interrupts abort twi power-down twi power-down operation may be aborted by other interrupts. if an interrupt (e.g., int0) occurs during twi power-down address watch and wakes the cpu up, the twi aborts operation and returns to its idle state. problem fix/workaround ensure that the twi address match is the only enabled interrupt when entering power-down. 4. twi master does not accept spikes on bus lines when the part operates as master, and the bus is idle (sda = 1; scl = 1), generat- ing a short spike on sda (sda = 0 for a short interval), no interrupt is generated, and the status code is still $f8 (idle). bu t when the software initiates a new start condition and clears twint, nothing happens on sda or scl, and twint is never set again. problem fix/workaround either of the following: 1. ensure that no spikes occur on sda or scl lines. 2. receiving a valid start condition followed by a stop condition provokes a bus error reported as a twi interrupt with status code $00. 3. in a single master systems, the user should write the twsto bit immedi- ately before writing the twsta bit. 3. twcr write operation ignored repeated write to twcr must be delayed. if a write operation to twcr is immedi- ately followed by another write operation to twcr, the first write operation may be ignored.
181 atmega163(l) 1142e?avr?02/03 problem fix/workaround ensure at least one instruction (e.g., nop) is executed between two writes to twcr. 2. pwm not phase correct in phase-correct pwm mode, a change from ocrx = top to anything less than top does not change the ocx output. this gives a phase error in the following period. problem fix/workaround make sure this issue is not harmful to the application. 1. twi is speed limited in slave mode when the two-wire serial interface operates in slave mode, frames may be unde- tected if the cpu frequency is less than 64 times the bus frequency. problem fix/workaround ensure that the cpu frequency is at least 64 times the twi bus frequency.
182 atmega163(l) 1142e?avr?02/03 change log this section containes a log on the changes made to the data sheet for atmega163. all refereces to pages in change log, are referred to this document. changes from rev. 1142c-09/01 to rev. 1142d-09/02 1. added ?not recommend for new designs. use atmega16.?. changes from rev. 1142d-09/09 to rev. 1142e-02/03 1. updated table 52, ?boot reset fuse,? on page 136. 2. corrected pin numbers in figure 62 on page 113. 3. corrected a constant in the boot loader code example on page 141. 4. changed max bit rate for the twi from 400 khz to 217 khz. 5. removed redundant and harmful loop in a code example for slave receiver mode for the twi on page 96. 6. added agnd and avcc in figure 81 on page 145 and figure 86 on page 154. 7. updated the ?packaging information? on page 178. 8. added ?erratas? on page 180.
i atmega163(l) 1142e?avr?02/03 table of contents features................................................................................................. 1 pin configurations................................................................................ 2 description ............................................................................................ 3 block diagram....................................................................................... 3 pin descriptions.................................................................................................... 4 clock options ....................................................................................................... 5 timer oscillator..................................................................................................... 6 architectural overview......................................................................... 7 the general purpose register file .................................................................... 10 the alu ? arithmetic logic unit......................................................................... 11 the in-system self-programmable flash program memory.............................. 11 the sram data memory.................................................................................... 11 the program and data addressing modes ........................................................ 12 the eeprom data memory .............................................................................. 16 memory access times and instruction execution timing .................................. 16 i/o memory ......................................................................................................... 17 reset and interrupt handling.............................................................................. 21 sleep modes....................................................................................................... 35 calibrated internal rc oscillator ........................................................................ 37 timer/counters ................................................................................... 39 timer/counter prescalers ................................................................................... 39 8-bit timer/counter0........................................................................................... 40 16-bit timer/counter1......................................................................................... 42 8-bit timer/counter 2.......................................................................................... 51 watchdog timer.................................................................................. 60 eeprom read/write access............................................................. 62 preventing eeprom corruption ........................................................................ 64 serial peripheral interface ? spi........................................................ 65 ss pin functionality............................................................................................ 66 data modes ........................................................................................................ 67 uart.................................................................................................... 70 data transmission.............................................................................................. 70 data reception ................................................................................................... 72 uart control ..................................................................................................... 74 double speed transmission............................................................................... 78
ii atmega163(l) 1142e?avr?02/03 two-wire serial interface (byte oriented) ........................................ 80 two-wire serial interface modes ........................................................................ 85 master transmitter mode.................................................................................... 86 master receiver mode........................................................................................ 86 slave receiver mode.......................................................................................... 87 slave transmitter mode...................................................................................... 88 miscellaneous states.......................................................................................... 88 the analog comparator................................................................... 102 analog comparator multiplexed input .............................................................. 104 analog to digital converter ............................................................. 105 feature list....................................................................................................... 105 operation .......................................................................................................... 106 prescaling and conversion timing ................................................................... 107 adc noise canceler function.......................................................................... 109 scanning multiple channels ............................................................................. 113 adc noise canceling techniques ................................................................... 113 adc characteristics ......................................................................................... 114 i/o ports............................................................................................. 115 port a................................................................................................................ 115 port b................................................................................................................ 117 port c................................................................................................................ 123 port d................................................................................................................ 128 memory programming...................................................................... 134 boot loader support......................................................................................... 134 self-programming the flash ............................................................................. 136 preventing flash corruption ............................................................................. 141 program and data memory lock bits............................................................... 143 fuse bits........................................................................................................... 144 signature bytes ................................................................................................ 144 calibration byte ................................................................................................ 144 parallel programming ....................................................................................... 145 parallel programming characteristics .............................................................. 153 serial downloading........................................................................................... 154 serial programming characteristics ................................................................. 159 electrical characteristics................................................................. 160 absolute maximum ratings*............................................................................. 160 external clock drive waveforms .................................................... 161 external clock drive......................................................................... 162
iii atmega163(l) 1142e?avr?02/03 two-wire serial interface characteristics ...................................... 163 typical characteristics .................................................................... 165 register summary ............................................................................ 172 instruction set summary ................................................................. 174 ordering information........................................................................ 177 packaging information ..................................................................... 178 44a ................................................................................................................... 178 40p6 ................................................................................................................. 179 erratas ............................................................................................... 180 atmega163(l) errata rev. f ........................................................................... 180 change log ....................................................................................... 182 changes from rev. 1142c-09/01 to rev. 1142d-09/02................................... 182 changes from rev. 1142d-09/09 to rev. 1142e-02/03 ................................... 182 table of contents .................................................................................. i
iv atmega163(l) 1142e?avr?02/03
printed on recycled paper. ? atmel corporation 2003. atmel corporation makes no warranty for the use of its products, other than those expressly contained in the company?s standard warranty which is detailed in atmel?s terms and conditions located on the company?s web site. the company assumes no responsibility for any errors which may appear in this document, reserves the right to change devices or specifications detailed herein at any time without n otice, and does not make any commitment to update the information contained herein. no licenses to patents or other intellectual property of at mel are granted by the company in connection with the sale of atmel products, expressly or by implication. atmel?s products are not authorized for use as critical components in life support devices or systems. atmel headquarters atmel operations corporate headquarters 2325 orchard parkway san jose, ca 95131 tel 1(408) 441-0311 fax 1(408) 487-2600 europe atmel sarl route des arsenaux 41 case postale 80 ch-1705 fribourg switzerland tel (41) 26-426-5555 fax (41) 26-426-5500 asia room 1219 chinachem golden plaza 77 mody road tsimhatsui east kowloon hong kong tel (852) 2721-9778 fax (852) 2722-1369 japan 9f, tonetsu shinkawa bldg. 1-24-8 shinkawa chuo-ku, tokyo 104-0033 japan tel (81) 3-3523-3551 fax (81) 3-3523-7581 memory 2325 orchard parkway san jose, ca 95131 tel 1(408) 441-0311 fax 1(408) 436-4314 microcontrollers 2325 orchard parkway san jose, ca 95131 tel 1(408) 441-0311 fax 1(408) 436-4314 la chantrerie bp 70602 44306 nantes cedex 3, france tel (33) 2-40-18-18-18 fax (33) 2-40-18-19-60 asic/assp/smart cards zone industrielle 13106 rousset cedex, france tel (33) 4-42-53-60-00 fax (33) 4-42-53-60-01 1150 east cheyenne mtn. blvd. colorado springs, co 80906 tel 1(719) 576-3300 fax 1(719) 540-1759 scottish enterprise technology park maxwell building east kilbride g75 0qr, scotland tel (44) 1355-803-000 fax (44) 1355-242-743 rf/automotive theresienstrasse 2 postfach 3535 74025 heilbronn, germany tel (49) 71-31-67-0 fax (49) 71-31-67-2340 1150 east cheyenne mtn. blvd. colorado springs, co 80906 tel 1(719) 576-3300 fax 1(719) 540-1759 biometrics/imaging/hi-rel mpu/ high speed converters/rf datacom avenue de rochepleine bp 123 38521 saint-egreve cedex, france tel (33) 4-76-58-30-00 fax (33) 4-76-58-34-80 e-mail literature@atmel.com web site http://www.atmel.com 1142e?avr?02/03 0m atmel ? and avr ? are the registered trademarks of atmel. other terms and product names may be the trademarks of others.


▲Up To Search▲   

 
Price & Availability of ATMEGA163L-4AC

All Rights Reserved © IC-ON-LINE 2003 - 2022  

[Add Bookmark] [Contact Us] [Link exchange] [Privacy policy]
Mirror Sites :  [www.datasheet.hk]   [www.maxim4u.com]  [www.ic-on-line.cn] [www.ic-on-line.com] [www.ic-on-line.net] [www.alldatasheet.com.cn] [www.gdcy.com]  [www.gdcy.net]


 . . . . .
  We use cookies to deliver the best possible web experience and assist with our advertising efforts. By continuing to use this site, you consent to the use of cookies. For more information on cookies, please take a look at our Privacy Policy. X